// File: file_io1b.java // // Author: Rahul Simha // Created: Jan 20, 2003 // // Illustrates reading from a file using a while. // A file is read line-by-line and each line is // printed to the screen. import java.io.*; public class file_io1b { public static void main (String[] argv) { try { // These are the key steps in setting up the read operation. FileReader fr = new FileReader ("testdata"); LineNumberReader lr = new LineNumberReader (fr); // Now read the input lines String input_line = lr.readLine (); int i = 0; while (input_line != null) { i ++; System.out.println ("Line " + i + ": " + input_line); input_line = lr.readLine (); } // Done. lr.close(); } catch (IOException e) { // If there was a problem... System.out.println (e); } } }