Java - FilterInputStream reset() method



Description

The Java FilterInputStream reset() method moves the stream back to the last marked position. It is used when you want to re-read data from an earlier point in the input stream. Works only if the stream supports marking (markSupported() must return true). Moves the stream back to the last mark(int readlimit) position. If no mark is set, calling reset() may throw an IOException.

Declaration

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

public void reset()

Parameters

NA

Return Value

The method does not return any value.

Exception

  • IOException − If the mark is invalidated or the stream is not marked.

Example - Usage of FilterInputStream reset() method

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

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FilterInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null; 
      FilterInputStream fis = null;
            
      try {
         // create input streams
         is = new FileInputStream("test.txt");
         fis = new BufferedInputStream(is);
         
         // reads and prints filter input stream
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
         // mark invoked at this position
         fis.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
         // reset() repositioned the stream to the mark
         fis.reset();
         System.out.println("reset() invoked");
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
      } catch(IOException e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

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 - Using reset() with BufferedInputStream

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

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + fis.markSupported());

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

         // Mark the current position
         fis.mark(5); // Can read up to 5 bytes before mark expires

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

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

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

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

Output(assuming 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 BufferedInputStream, which supports mark() and reset().

  • Reads one character, then marks the stream.

  • Reads two more characters, then resets back.

  • Reads again from the marked position.

Example - Handling reset() Without a Mark Using PushbackInputStream

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

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new PushbackInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + fis.markSupported());

         // Trying to reset without marking (causes IOException)
         fis.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 PushbackInputStream, which does NOT support mark() and reset().

  • Calls markSupported(), which returns false.

  • Calls reset() without marking, causing an IOException.

java_io_filterinputstream.htm
Advertisements