/****************************************************************************** * This program simply copies one file to another, character by * character like the Unix 'cat' program. * * 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 FileIO7 { 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")); int c; while ((c = ifh.read()) != -1) { ofh.write(c); } 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: ", "FileIO7", " {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))); } } }