Java.io.FilterInputStream.reset() Method

Advertisements


Description

The java.io.FilterInputStream.reset() method repositions this stream to the position at the time the mark() method was last invoked on this input stream.

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.l

Example

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

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("C://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();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

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



Advertisements
Advertisements