/****************************************************************************** * This program simply copies a file to the console line by line. * * 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 FileIO2 { 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!"); } String line; while ((line = ifh.readLine()) != null) { System.out.println(line); } try {ifh.close();} catch (IOException ex) {}; } public static void main(String[] args) { if (args.length != 1) { System.out.println(Utils.join("", "Syntax: ", "FileIO2", " {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"); } } }