import java.io.*; /** * List.java *
A class to demnstrate File I/O *
* Created: Fri Feb 7 08:08:52 2003 * * @author Stuart C. Shapiro */ public class List { /** * Lists a file on System.out
* The file name must be supplied as a command-line argument. *
* Prints an appropriate message if the file doesn't exist. *
Prints an appropriate message if there is an I/O error during operation. * * @param args a String[] value *
* arg[0] is used as the name of the file to be printed. *
* Other command-line arguments are ignored. */ public static void main (String[] args) { String filename = ""; String input; try { filename = args[0]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("A file name must be supplied."); System.exit(1); } // end of try-cach try { BufferedReader reader = new BufferedReader(new FileReader(filename)); System.out.println(filename); for (int i = 0; i < filename.length(); i++) { System.out.print("="); } // end of for System.out.println(); while ( (input = reader.readLine()) != null ) { System.out.println(input); } // end of while reader.close(); } catch (FileNotFoundException e) { System.out.println("File \"" + filename + "\" not found."); } catch (IOException e) { System.out.println("I/O error."); } // end of try-catch } // end of main () }// List