/****************************************************************************** * This program simply copies one file to another, line by line. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.FileSystems; import java.nio.file.Files; import org.pureprogrammer.Utils; public class FileIO8 { static void copyTextFile(String fromFilespec, String toFilespec) throws IOException { BufferedReader ifh = Files.newBufferedReader(FileSystems.getDefault().getPath(fromFilespec), Charset.forName("UTF-8")); BufferedWriter ofh = Files.newBufferedWriter(FileSystems.getDefault().getPath(toFilespec), Charset.forName("UTF-8")); String line; while ((line = ifh.readLine()) != null) { ofh.write(line);ofh.newLine(); } try {ifh.close();} catch (IOException ex) {}; try {ofh.close();} catch (IOException ex) {}; } public static void main(String[] args) { if (args.length != 2) { System.out.println(Utils.join("", "Syntax: ", "FileIO8", " {fromFilespec} {toFilespec}")); System.exit(1); } String fromFilespec = args[0]; String toFilespec = args[1]; try { copyTextFile(fromFilespec, toFilespec); } catch (IOException ex) { System.out.println(Utils.join("", "Error: ", Utils.exceptionMessage(ex))); } } }