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






Using the LineNumberReader to read a text file line by line

    

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();
  }
}

   
    
    
    
  








Related examples in the same category

1.Create LineNumberReader from FileReader
2.Line Number IO
3.Use LineNumberReader class to read file
4.Split Lines
5.Convert lines into the canonical format, that is, terminate lines with the CRLF sequence.