Chain a line number reader onto a file reader. : Reader « File « SCJP






import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class MainClass {
  public static void main(String[] argv) {
    try {
      FileReader fr = new FileReader("data");
      LineNumberReader lnr = new LineNumberReader(fr);
      String s;

      while ((s = lnr.readLine()) != null) {
        System.out.println(lnr.getLineNumber() + " : " + s);
      }
      lnr.close();
      fr.close();
    } catch (IOException x) {
    }

  }
}








9.4.Reader
9.4.1.Chain a line number reader onto a file reader.
9.4.2.Using FileWriter and FileReader