Using the LineNumberReader to read a text file line by line : LineNumberReader « File « Java Tutorial






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

public class Main {
  public static void main(String[] args) throws Exception {
    LineNumberReader r = new LineNumberReader(new FileReader("yourFile.txt"));

    String line = null;

    while ((line = r.readLine()) != null) {
      System.out.println(r.getLineNumber() + ": " + line);
    }
    r.close();
  }
}








11.37.LineNumberReader
11.37.1.Using the LineNumberReader to read a text file line by line
11.37.2.Compare text file line by line
11.37.3.Use LineNumberReader class to read file