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 the 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)
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.
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)
public void setLineNumber(int lineNumber)
2-getLineNumber :
This method returns the current line number. It has no argument.
Syntax
public int getLineNumber()
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()
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
public int read(char[] cbuf,int off,int len)throws IOException
5- readLine :


Join the conversation! Your thoughts help the community grow.