Read a text file line by line using the LineNumberReader in Java
Description
The following code shows how to read a text file line by line using the LineNumberReader.
Example
/* w ww . j a v a2s . c o m*/
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();
}
}
The code above generates the following result.