Java - LineNumberReader Class



Introduction

The Java LineNumberReader class is a buffered character-input stream that keeps track of line numbers.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.

Class declaration

Following is the declaration for Java.io.LineNumberReader class −

public class LineNumberReader
   extends BufferedReader

Field

Following are the fields for Java.io.LineNumberReader class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

LineNumberReader(Reader in)

This creates a new line-numbering reader, using the default input-buffer size.

2

LineNumberReader(Reader in, int sz)

This creates a new line-numbering reader, reading characters into a buffer of the given size.

Class methods

Sr.No. Method & Description
1 int getLineNumber()

This method gets the current line number.

2 void mark(int readAheadLimit)

This method mark the present position in the stream.

3 int read()

This method read a single character.

4 int read(char[] cbuf, int off, int len)

This method read characters into a portion of an array.

5 String readLine()

This method read a line of text.

6 void reset()

This method reset the stream to the most recent mark.

7 void setLineNumber(int lineNumber)

This method set the current line number.

8 long skip(long n)

This method skip characters.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.BufferedReader
  • Java.io.Reader
  • Java.io.Object

Example - Reading a File with Line Numbers

The following example shows the usage of Java LineNumberReader getLineNumber() method. This example reads a text file line by line and prints the current line number.

LineNumberReaderDemo.java

package com.tutorialspoint;

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

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      try {
         // Create a LineNumberReader from a file
         LineNumberReader lnr = new LineNumberReader(new FileReader("example.txt"));

         String line;
         while ((line = lnr.readLine()) != null) {
            System.out.println("Line " + lnr.getLineNumber() + ": " + line);
         }

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains multiple lines.)

Let us compile and run the above program, this will produce the following result−

Line 1: Hello
Line 2: World
Line 3: Java Programming

Explanation

  • LineNumberReader reads lines from "example.txt".

  • readLine() reads a line, and getLineNumber() returns the current line number.

  • The line number starts at 1 and increments automatically.

Example - Marking and Resetting a Line

The following example shows the usage of Java LineNumberReader mark(int readAheadLimit) method. This example demonstrates marking a position in the stream, reading some lines, and then using reset() to go back to the marked position.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "First Line\nSecond Line\nThird Line";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

      try {
         System.out.println("Reading first line: " + lnr.readLine());

         // Mark the current position
         lnr.mark(50);
         System.out.println("Marked at Line Number: " + lnr.getLineNumber());

         System.out.println("Reading second line: " + lnr.readLine());
         System.out.println("Reading third line: " + lnr.readLine());

         // Reset to the marked position
         lnr.reset();
         System.out.println("After reset, current line: " + lnr.readLine());
         System.out.println("Line Number after reset: " + lnr.getLineNumber());

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

Let us compile and run the above program, this will produce the following result−

Reading first line: First Line
Marked at Line Number: 1
Reading second line: Second Line
Reading third line: Third Line
After reset, current line: Second Line
Line Number after reset: 2

Explanation

  • Reads the first line (First Line).

  • Calls mark(50), saving the position at line 1.

  • Reads two more lines (Second Line and Third Line).

  • Calls reset(), which returns to the marked position.

  • Reads again from the marked line (Second Line), confirming that reset() worked.

Example - Reading Characters One by One

The following example shows the usage of Java LineNumberReader read() method. This example reads characters from a LineNumberReader one at a time and prints their ASCII values along with the current line number.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "Hello\nWorld\nJava";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

      try {
         int character;
         while ((character = lnr.read()) != -1) { // Read one character at a time
            System.out.println("Character: " + (char) character + ", Line Number: " + lnr.getLineNumber());
         }

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Character: H, Line Number: 0
Character: e, Line Number: 0
Character: l, Line Number: 0
Character: l, Line Number: 0
Character: o, Line Number: 0
Character: 
, Line Number: 1
Character: W, Line Number: 1
Character: o, Line Number: 1
Character: r, Line Number: 1
Character: l, Line Number: 1
Character: d, Line Number: 1
Character: 
, Line Number: 2
Character: J, Line Number: 2
Character: a, Line Number: 2
Character: v, Line Number: 2
Character: a, Line Number: 2

Explanation

  • The read() method reads characters one at a time.

  • Line number starts at 0 and increments when a newline (\n) is encountered.

  • The program prints the character and its corresponding line number.

Advertisements