Java - FilterReader reset() method



Description

The Java FilterReader reset() method moves the stream back to the last marked position, allowing the reader to re-read the data. It works only if the stream supports marking (markSupported() returns true) and mark(int readAheadLimit) was called before reset(). Moves the reader back to the last mark() position. If mark(int readAheadLimit) was not called, reset() throws an IOException. Works only if markSupported() returns true.

Declaration

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

public void reset()

Parameters

NA

Return Value

The method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterReader reset() method

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      FilterReader fr = null;
      Reader r = null;
            
      try {
         // create new reader
         r = new StringReader("ABCDEF");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // read and print characters one by one
         System.out.println("Char : "+(char)fr.read());
         System.out.println("Char : "+(char)fr.read());
         System.out.println("Char : "+(char)fr.read());
            
         // mark is set on the filter reader
         fr.mark(0);
         System.out.println("Char : "+(char)fr.read());
         System.out.println("reset() invoked");
            
         // reset is called
         fr.reset();
            
         // read and print characters
         System.out.println("char : "+(char)fr.read());
         System.out.println("char : "+(char)fr.read());
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Output

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

Char : A
Char : B
Char : C
Char : D
reset() invoked
char : D
char : E

Example - Using reset() with BufferedReader

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
         System.out.println("Mark supported? " + fr.markSupported());

         // Read and print first character
         System.out.print((char) fr.read());

         // Mark the current position
         fr.mark(5); // Can read up to 5 characters before mark becomes invalid

         // Read next two characters
         System.out.print((char) fr.read());
         System.out.print((char) fr.read());

         // Reset back to the marked position
         fr.reset();

         // Read again from the marked position
         System.out.print((char) fr.read());
         System.out.print((char) fr.read());

      } 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−

Mark supported? True
Helo
elo

Explanation

  • Uses BufferedReader, which supports marking.

  • Calls markSupported() to check if mark() and reset() are supported.

  • Marks the position after reading one character.

  • Reads two more characters, then resets to the marked position.

  • Reads again from the marked position.

Example - Handling reset() Without Marking Using PushbackReader

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PushbackReader;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (FilterReader fr = new PushbackReader(new FileReader("example.txt"))) {
         System.out.println("Mark supported? " + fr.markSupported());

         // Trying to reset without marking (causes IOException)
         fr.reset();

      } catch (IOException e) {
         System.out.println("Reset failed: " + e.getMessage());
      }
   }
}

Output

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

Mark supported? False
Reset failed: mark/reset not supported

Explanation

  • Uses PushbackReader, which does not support marking.

  • Calls markSupported(), which returns false.

  • Calls reset() without marking, which throws an IOException.

java_io_filterreader.htm
Advertisements