Introduction
In this article we are going to describe how to use the Java LineNumberReader Class. I describe its constructor in detail and its method details with syntax. This class is found in the io package. And this class extends the BufferReader Class. A buffered character-input stream that keeps track of line numbers. This class defines the methods void setLineNumber(int) and int getLineNumber() for setting and getting the current line number respectively.
Usually you use the readLine() method to provide by the FileReader class. The readLine() method is used to read data line by line. The LineNumberReader class provides the getLineNumber() method to get the number of a line. We are using the File(String file_name) constructor to make an object of a file. We are reading the file by the use of the FileReader(File file) constructor. We are using the LineNumberReader(FileReader filereader) constructor to create an object of the LineNumberReader class.
Constructor details
This class has two types of constructors:
1-LineNumberReader(Reader in) :
This constructor takes only one argument for the Reader class object and it creates it using the default input buffer size.
Syntax
public LineNumberReader(Reader in)
2-LineNumberReader(Reader in,int sz) :
This constructor has two arguments; first a Reader class object and the second argument is the input buffer size. It provides flexibility by giving the buffer size as an integer.
Syntax
public LineNumberReader(Reader in,int sz)
Method Details
1-setLineNumber : This method helps to set the current line and it takes an integer argument as the line number that you want to set.
Syntax
public void setLineNumber(int lineNumber)
2-getLineNumber : This method returns the current line number. It has no argument.
Syntax
public int getLineNumber()
3-read : This read method reads a single character. Line terminators are compressed into single newline ('\n') characters. And this method is derived from the super class BufferReader.
Syntax
public int read()
4-read : This method takes two arguments. Read characters into a portion of an array. It takes three arguments; cbuf - Destination buffer, off - Offset at which to start storing characters, len - Maximum number of characters to read. And it throws IOException.
Syntax
public int read(char[] cbuf,int off,int len)throws IOException
5- readLine : This method reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Syntax
public String readLine()throws IOException
7-skip :This method is used to skip character. It take the argument long n - The number of characters to skip.
Syntax
public long skip(long n)throws IOException
8-mark : This method is used to mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point, and will also reset the line number appropriately.
Syntax
public void mark(int readAheadLimit)throws IOException
9-reset : This method is used to reset the stream to the most recent mark.
Syntax
public void reset()throws IOException
Example
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class LineCounterDemo {
public static void main(String[] args) throws Exception {
LineNumberReader lineCounter = new LineNumberReader(
new InputStreamReader(System.in));
String nextLine = null;
System.out.println("Type any text and press return. Type 'exit' to quit the program.");
try {
while ((nextLine = lineCounter.readLine()).indexOf("exit") == -1) {
if (nextLine == null)
break;
System.out.print(lineCounter.getLineNumber());
System.out.print(": ");
System.out.println(nextLine);
}
} catch (Exception done) {
done.printStackTrace();
}
}
}
OUTPUT