|
Line Number Reader
Print a text file line by line along with line numbers.
|
package com.bethecoder.tutorials.coding.examples;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class LineNumberReaderTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileReader freader = null;
LineNumberReader lnreader = null;
try {
freader = new FileReader("C:/abcd.txt");
lnreader = new LineNumberReader(freader);
String line = null;
while ((line = lnreader.readLine()) != null) {
System.out.println("Line#" + lnreader.getLineNumber() + ": " + line);
}
} finally {
freader.close();
lnreader.close();
}
}
}
|
| |
It gives the following output,
Line#1: ABCD
Line#2: ABCD
Line#3: ABCD
Line#4: ABCD
|
|