Use LineNumberReader class to read file : LineNumberReader « File « Java Tutorial






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

public class Main {
  public static void main(String[] args) throws Exception {
    File file = new File("data.csv");
    FileReader fr = new FileReader(file);
    LineNumberReader lnr = new LineNumberReader(fr);
    // lnr.setLineNumber(400);
    String line = "";
    while ((line = lnr.readLine()) != null) {
      System.out.println("Line Number " + lnr.getLineNumber() + ": " + line);
    }
    fr.close();
    lnr.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