For reading a file line by line, the LineNumberReader
class could be a perfect choice. It is a buffered character-input stream that keeps track of line numbers.
By default, line numbering begins at 0. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), combination of both (‘\r\n’) or any of the previous terminators followed by the end of stream, or end of stream not preceded by another terminator.
1. How Does LineNumberReader Works?
LineNumberReader is a subclass of the BufferedReader
class and allows us to keep track of which line we are currently processing.
Line numbering begins at 0 (similar to array indices). Whenever the LineNumberReader
encounters a line terminator by the wrapped Reader
, the line number is incremented. At the end of the stream, the line number is incremented last time.
The LineNumberReader provides the following important methods:
getLineNumber()
: gets the current line number where the reader is reading.setLineNumber(int lineNumber)
: sets the current line number. Note that this method does not change the current position of reader in the stream; it only changes the value that will be returned by getLineNumber().readLine()
: reads the current line, not including any line termination characters, or null if the end of the stream has been reached.reset()
: resets the stream to the most recent mark created by the mark() method.mark(n)
: marks the present position in the stream. Calling reset() will attempt to reposition the stream to this point, and will also reset the line number appropriately.
LineNumberReader
can be useful if we are parsing a text file that can contain errors. When reporting the error to the user, it is easier to correct the error if the error message includes the line number.
2. Using LineNumberReader
Let’s build a quick example to show the capabilities of LineNumberReader
. This is the file content that I will use to read using LineNumberReader
in the below example.
firstName=Lokesh
lastName=Gupta
blog=howtodoinjava
technology=java
In the given example, we are iterating over the lines using the method lineNumberReader.readLine()
until it returns null
. A null
value means that all the lines in the file have been read.
final String fileName = "app.properties";
try(LineNumberReader lineNumberReader
= new LineNumberReader(new FileReader(filename))) {
//Print initial line number
System.out.println("Line " + lineNumberReader.getLineNumber());
//Setting initial line number
lineNumberReader.setLineNumber(5);
//Get current line number
System.out.println("Line " + lineNumberReader.getLineNumber());
//Read all lines now; Every read increase the line number by 1
String line = null;
while ((line = lineNumberReader.readLine()) != null)
{
System.out.println("Line " + lineNumberReader.getLineNumber() + ": " + line);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
Program Output:
Line 0 Line 5 Line 6: firstName=Lokesh Line 7: lastName=Gupta Line 8: blog=howtodoinjava Line 9: technology=java
Happy Learning !!