Java - LineNumberReader reset() method



Description

The Java LineNumberReader reset() method restores the stream to the last marked position. This allows re-reading lines from a previously marked position while keeping track of line numbers.

Important

  • Before calling reset(), you must call mark(int readAheadLimit) to mark a position.

  • If the read-ahead limit is exceeded, reset() may throw an IOException.

Declaration

Following is the declaration for java.io.LineNumberReader.reset() method −

public void reset()

Parameters

NA

Return Value

The method does not return any value.

Exception

IOException − If the stream has not been marked, or if the mark has been invalidated.

Example - Usage of LineNumberReader reset() method

The following example shows the usage of Java LineNumberReader reset() method.

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) throws IOException {
      FileReader fr = null;
      LineNumberReader lnr = null;
         
      try {
         // create new reader
         fr = new FileReader("test.txt");
         lnr = new LineNumberReader(fr);
         
         // reads and prints data from reader
         System.out.println((char)lnr.read());
         System.out.println((char)lnr.read());
         
         // mark invoked at this position
         lnr.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)lnr.read());
         System.out.println((char)lnr.read());
         
         // if this reader supports mark() an reset()
         if(lnr.markSupported()) {
         
            // reset() repositioned the stream to the mark
            lnr.reset();
            System.out.println("reset() invoked");
            System.out.println((char)lnr.read());
            System.out.println((char)lnr.read());
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases system resources
         if(fr!=null)
            fr.close();
         if(lnr!=null)
            lnr.close();
      }
   }
}

Output(Assuming test.txt contains multiple lines)

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

A
B
mark() invoked
C
D
reset() invoked
C
D

Example - Resetting to a Marked Position

The following example shows the usage of Java LineNumberReader reset() method. This example marks a position in the stream, reads some lines, and then resets to re-read from 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 = "Line 1\nLine 2\nLine 3\nLine 4";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

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

         // Mark position at Line 2
         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 (before reading Line 2)
         lnr.reset();
         System.out.println("After reset, re-reading: " + lnr.readLine());
         System.out.println("Line Number after reset: " + lnr.getLineNumber());

         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−

Reading first line: Line 1
Marked at Line Number: 1
Reading second line: Line 2
Reading third line: Line 3
After reset, re-reading: Line 2
Line Number after reset: 2

Explanation

  • Reads "Line 1" and then marks the position before "Line 2".

  • Reads two more lines ("Line 2" and "Line 3").

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

  • Reads "Line 2" again, confirming that reset() worked.

Example - Resetting After Skipping Lines

The following example shows the usage of Java LineNumberReader reset() method. This example marks a position, skips a line, and then resets to re-read from 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 = "Apple\nBanana\nCherry\nDate";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

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

         // Mark position before reading the second line
         lnr.mark(100);
         System.out.println("Marked at Line Number: " + lnr.getLineNumber());

         // Read next line and skip another line
         System.out.println("Reading second line: " + lnr.readLine());
         lnr.readLine(); // Skip "Cherry"

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

         // Reset back to the marked position (before "Banana")
         lnr.reset();
         System.out.println("After reset, reading again: " + lnr.readLine());
         System.out.println("Line Number after reset: " + lnr.getLineNumber());

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

Output

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

Reading first line: Apple
Marked at Line Number: 1
Reading second line: Banana
Reading fourth line: Date
After reset, reading again: Banana
Line Number after reset: 2

Explanation

  • Reads "Apple", then marks the position before "Banana".

  • Reads "Banana", then skips "Cherry" and reads "Date".

  • Calls reset(), restoring the position before "Banana".

  • Re-reads "Banana", confirming that reset() worked.

java_io_linenumberreader.htm
Advertisements