Working With LineNumberReader Class in Java


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.
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.
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.
public void setLineNumber(int lineNumber)
2-getLineNumber :
This method returns the current line number. It has no argument.
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.
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.
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.
public String readLine()throws IOException
7-skip : 
This method is used to skip character. It takes the argument long n - The number of characters to skip.
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.
public void mark(int readAheadLimit)throws IOException
9-reset :
This method is used to reset the stream to the most recent mark.
public void reset()throws IOException

  1. import;  
  2. import;  
  4. public class LineCounterDemo {  
  6.       public static void main(String[] args) throws Exception {  
  7.             LineNumberReader lineCounter = new LineNumberReader(  
  8.                         new InputStreamReader(;  
  10.             String nextLine = null;  
  11.             System.out.println("Type any text and press return. Type 'exit' to quit the program.");  
  12.             try {  
  13.                   while ((nextLine = lineCounter.readLine()).indexOf("exit") == -1) {  
  14.                         if (nextLine == null)  
  15.                               break;  
  16.                         System.out.print(lineCounter.getLineNumber());  
  17.                         System.out.print(": ");  
  18.                         System.out.println(nextLine);  
  19.                   }  
  20.             } catch (Exception done) {  
  21.                   done.printStackTrace();  
  22.             }  
  23.       }  
  24. }  
 cmdlinereader class.jpg