Java - BufferedReader reset() method



Description

The Java BufferedReader reset() method resets the stream to the most recent mark set by the mark(int readAheadLimit) method. If the mark() method was not called before reset(), or if the read limit specified in mark(int readAheadLimit) has been exceeded, calling reset() will throw an IOException.

Declaration

Following is the declaration for java.io.BufferedReader.ready() method.

public void reset()

Parameters

NA

Return Value

This method does not return any value.

Exception

IOException− If the stream is never been marked, or the mark has become invalid.

Example - Using a BufferedReader reset() method

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

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      String s ="ABCDE";
      StringReader sr = null; 
      BufferedReader br = null;

      try {
         sr = new StringReader(s);
         
         // create new buffered reader
         br = new BufferedReader(sr);

         // reads and prints BufferedReader
         System.out.println((char)br.read());
         System.out.println((char)br.read());
         
         // mark invoked at this position
         br.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)br.read());
         System.out.println((char)br.read());
         
         // reset() repositioned the stream to the mark
         br.reset();
         System.out.println("reset() invoked");
         System.out.println((char)br.read());
         System.out.println((char)br.read());
         
      } catch (Exception e) {
         // exception occurred.
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(sr!=null)
            sr.close();
         if(br!=null)
            br.close();
      }
   }
}

Output

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 the Reader to Re-read Content

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

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      String input = "Line 1: Java\nLine 2: BufferedReader\nLine 3: reset() Example";

      // Initialize BufferedReader with a StringReader
      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         // Read and print the first line
         System.out.println("First read:");
         System.out.println(reader.readLine());

         // Mark the current position in the stream
         reader.mark(100); // Allow up to 100 characters to be read before reset becomes invalid

         // Read the next line
         System.out.println("Second read:");
         System.out.println(reader.readLine());

         // Reset the reader to the marked position
         reader.reset();

         // Re-read the line after reset
         System.out.println("Re-read after reset:");
         System.out.println(reader.readLine());
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

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

First read:
Line 1: Java
Second read:
Line 2: BufferedReader
Re-read after reset:
Line 2: BufferedReader

Explanation

  • The BufferedReader reads the input string line by line.

  • The mark(100) method marks the position after reading the first line, allowing up to 100 characters to be read before the mark becomes invalid.

  • After reading the second line, the reset() method is called to return the reader to the marked position.

  • The second line is re-read after resetting the stream.

Example - Resetting the Reader to Read a Specific Portion of the Input Again

The following example shows the usage of BufferedReader ready() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      String input = "1234567890";

      // Initialize BufferedReader with a StringReader
      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         char[] buffer = new char[5];

         // Mark the starting position
         reader.mark(10); // Allow up to 10 characters to be read before mark is invalid

         // Read the first 5 characters
         reader.read(buffer, 0, 5);
         System.out.println("First read: " + new String(buffer));

         // Reset to the marked position
         reader.reset();

         // Re-read the same 5 characters after reset
         reader.read(buffer, 0, 5);
         System.out.println("Re-read after reset: " + new String(buffer));
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

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

First read: 12345
Re-read after reset: 12345

Explanation

  • The BufferedReader reads a string of digits ("1234567890").

  • The mark(10) method marks the starting position in the stream, allowing up to 10 characters to be read before the mark becomes invalid.

  • The first 5 characters are read into a buffer, and the result is printed.

  • The reset() method resets the stream to the marked position, allowing the same 5 characters to be re-read.

  • The second read produces the same result as the first.

Key Points About reset()

  • Mark Requirement− You must call mark(int readAheadLimit) before using reset(), or an IOException will be thrown.

  • Mark Expiry− The readAheadLimit specified in mark() determines how many characters can be read before the mark becomes invalid.

  • Use Cases− The reset() method is useful for re-reading specific portions of input without reinitializing the stream.

These examples demonstrate how reset() works in conjunction with mark() to allow re-reading input efficiently.

java_io_bufferedreader.htm
Advertisements