/****************************************************************************** * This program simply copies a file to the console character by * character like the Unix 'cat' program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import java.io.BufferedReader; 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 FileIO1 { static void readTextFile(String filespec) throws IOException { BufferedReader ifh = Files.newBufferedReader(FileSystems.getDefault().getPath(filespec), Charset.forName("UTF-8")); if (!ifh.ready()) { throw new IOException("Problem opening input file!"); } int c; while ((c = ifh.read()) != -1) { System.out.print(String.valueOf(Character.toChars(c))); } try {ifh.close();} catch (IOException ex) {}; } public static void main(String[] args) { if (args.length != 1) { System.out.println(Utils.join("", "Syntax: ", "FileIO1", " {filename}")); System.exit(1); } final String FILESPEC = args[0]; try { readTextFile(FILESPEC); } catch (IOException ex) { System.out.println(Utils.join("", "Error: ", Utils.exceptionMessage(ex))); } catch (Exception ex) { System.out.println("Unexpected File Read Error"); } } }