Java IO LineNumberReader

In this tutorial we will learn about the LineNumberReader in Java.

Java IO LineNumberReader

In this tutorial we will learn about the LineNumberReader in Java.

Java IO LineNumberReader

Java IO LineNumberReader

In this tutorial we will learn about the LineNumberReader in Java.

java.io.LineNumberReader class extends the java.io.BufferedReader. In the implementation of LineNumberReader class character-input streams are kept into a buffer and this class keeps the record of line numbers of these buffered streams. By default, counting of line number is started at 0 (zero) however, this value is incremented at the every line terminator (\n) when reading the data from stream. Value of line number can be get by the getLineNumber() method and the line number can also be set by the setLineNumber() method. When value of line number is set by the setLineNumber() it doesn't mean that the current position in the stream is changed, it changes only the value which is get by the getLineNumber() method.

Constructor of LineNumberReader

Constructor
Description
LineNumberReader(Reader in) This constructor creates a new LineNumberReader which contains object of a Reader (input stream).
Syntax : public LineNumberReader(Reader in)
LineNumberReader(Reader in, int sz) This constructor creates a new LineNumberReader which contains object of a Reader (input stream) that reads characters into a buffer with the specified given size.

Methods in LineNumberReader

  • getLineNumber() : This method is used to get the line number from stream when the data is being read.

    Syntax : public int getLineNumber()
     
  • mark(int readAheadLimit) : This method is used to mark the current position in the stream.

    Syntax : public void mark(int readAheadLimit) throws IOException
     
  • read() : This method is used to read a character from the stream.

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read the specified length of characters from the specified char array buffer starts at the specified position.

    Syntax : public int read(char[] cbuf, int off, int len) throws IOException
     
  • readLine() : This method is used to read a line of text.

    Syntax : public String readLine() throws IOException
     
  • reset() : This method is used to repositioned the marker marked by the mark() method.

    Syntax : public void reset() throws IOException
     
  • setLineNumber(int lineNumber) : This method is used to set the line number.

    Syntax : public void setLineNumber(int lineNumber)
     
  • skip(long n) : This method is used to skip the specified number of characters.

    Syntax : public long skip(long n) throws IOException

Example

Example for demonstrating about LineNumberReader is being given here. This is a very simple example which reads a line of text. In this example line of text will be read from the file and displayed to the console. In this example also retrieved the line number of each line of text.

Source Code

JavaLineNumberReaderExample.java

import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;

public class JavaLineNumberReaderExample
{
   public static void main(String args[])
    {
       FileReader fr = null;
       LineNumberReader lnr = null;
       try
         {
             fr = new FileReader("test.txt");
             lnr = new LineNumberReader(fr);
             //int r;
             String lText = "";
             System.out.println("Line Number \t Line of Text ");
             while((lText = lnr.readLine()) != null)
             {
                 int lNum =  lnr.getLineNumber();                 
	   System.out.println(lNum +"\t     "+ lText);
             }
         }
        catch(IOException ioe)
         {
            System.out.println(ioe);
         }
        finally
         {
            if(fr != null)
              {
                 try
                   {
                      fr.close();
                   }
                  catch(Exception e)
                   {
                      System.out.println(e);
                   }
              }
            if(lnr != null)
              {
                 try
                   {
                      lnr.close();
                   }
                  catch(Exception e)
                   {
                      System.out.println(e);
                   }
              }
         }
    }
}

Output

When you will execute the above example you will get the output as below. In this output line number is displayed and the text of each corresponding line number is also displayed.

Download Source Code