|
Java > IO > Line Number Reader |
|
Line Number Reader
The following example shows how to use LineNumberReader.
|
package com.bethecoder.tutorials.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class LineNumberReaderTest {
/**
* @param args
*/
public static void main(String[] args) {
LineNumberReader reader = null;
try {
reader = new LineNumberReader(new FileReader("C:\\abc.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println("Line#" + reader.getLineNumber() + ": " + line);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
| |
It gives the following output,
Line#1: Hello World
Line#2: WriteTextFile Example
Line#3: Hello World2
Line#4: Hello World3
|
|