Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java FilterInputStream reset() Method

In this tutorial, we will learn about the reset() method of FilterInputStream class in Java. This method is used to reposition the current Filter Input Stream to the point of the most recent invocation of the mark() method in the given stream. It is a non-static method, available in the java.io package.

Syntax

This is the syntax declaration of this method. It does not accept any parameters and the return type of this method is void i.e it does not return any value.

public void reset()

Example 1: Reset the Mark in Input Stream in Java

Here, we are implementing the reset() method to reset the pointer of the current stream to the recently marked position. Firstly, we mark the position of the current stream initially and then we read data from the file and then we reset the pointer to the stream by calling the method reset() and this will shift the pointer of the current stream to the recently marked position and the recently marked position is the first position so it will again start reading the data from the beginning.

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

public class StudyTonight
{
	public static void main(String args[])
	{
		 try
		 {			 
	            File file = new File("E:\\studytonight\\output.txt");
	 
	            FileInputStream fis = new FileInputStream(file);
	            FilterInputStream filterIs = new BufferedInputStream(fis);	 
	 
	            filterIs.mark(0);
	 
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	 
	            System.out.println("Last mark call at 0th index:- ");
	            filterIs.reset();
	            System.out.println("reset() called ");
	 
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	 
	            fis.close();
	            filterIs.close();
	 
	        } 
		 	catch (IOException ex) 
		 	{
	            ex.printStackTrace();
	        }
	}
}


H
e
Last mark call at 0th index:-
reset() called
H
e

output.txt

Hello Studytonight

Example 2: Reset the Mark in Input Stream in Java

In this example, we are reading the data and mark position and then just after marking the position we reset that position by calling the method reset() so apparently, it will not show any different output because the recently marked position and the current positions are same.

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

public class StudyTonight
{
	public static void main(String args[])
	{
		 try
		 {			 
	            File file = new File("E:\\studytonight\\output.txt");
	 
	            FileInputStream fis = new FileInputStream(file);
	            FilterInputStream filterIs = new BufferedInputStream(fis);	 	           
	 
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	            filterIs.mark(0);
	            filterIs.reset();
	            System.out.println("reset() called ");
	 
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	 
	            fis.close();
	            filterIs.close();
	 
	        } 
		 	catch (IOException ex) 
		 	{
	            ex.printStackTrace();
	        }
	}
}


H
e
reset() called
l
l
o

output.txt

Hello Studytonight

Example 3: Reset the Mark in Input Stream in Java

What will happen if we call the reset() method without calling any mark() method? This will throw an exception that is resetting to the invalid mark. Because in this case, we did not set any mark so reset() method is unaware of the last recently marked position.

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

public class StudyTonight
{
	public static void main(String args[])
	{
		 try
		 {			 
	            File file = new File("E:\\studytonight\\output.txt");
	 
	            FileInputStream fis = new FileInputStream(file);
	            FilterInputStream filterIs = new BufferedInputStream(fis);	 
	 
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	 
	           
	            filterIs.reset();
	            System.out.println("reset() called ");
	 
	            System.out.println((char) filterIs.read());
	            System.out.println((char) filterIs.read());
	 
	            fis.close();
	            filterIs.close();
	 
	        } 
		 	catch (IOException ex) 
		 	{
	            ex.printStackTrace();
	        }
	}
}


H
e
java.io.IOException: Resetting to invalid mark

Conclusion:

In this tutorial, we learned about the reset() method of FilterInputStream class in Java, which resets the particular file input stream to the position where the mark() method was called most recently in the given input stream. It is accessible with the class object only and if we try to access the method with the class name, we will get an error.