Signup/Sign In
PUBLISHED ON: MARCH 8, 2021

Java FilterReader reset() Method

In this tutorial, we will learn about the reset() method of the FilterReader class in Java. This method is called to reset the stream to the position, where the mark() method was called the most recently. It is a non-static method, available in the java.io package, which should only be accessed using class objects, otherwise accessing it with class name throws an IOException.

Syntax

This is the syntax declaration of this method. It does not accept any parameter or return any value, as its return type is void.

public void reset()

Example 1: Reset FilterReader in Java

In this example, we will illustrate the use of the reset() method, this method shifts the current reading position of the stream to the recently marked position. Here firstly we check whether this stream supports the mark() method and the marking the position at the beginning of the stream then we read the first five characters and then we call the reset() method so when we call the reset() method it will shift the pointer to the initial position of the stream and it will again start reading from the beginning of the stream.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Hello Studytonight"); 
			FilterReader fileReader = new FilterReader(reader){}; 
			char ch[] = new char[8]; 

			if(fileReader.markSupported()) 
			{ 
				fileReader.mark(100); 
			} 
			fileReader.read(ch); 
			for (int i = 0; i < 5; i++)  
			{ 
				System.out.print(ch[i]); 
			} 
			fileReader.reset(); 
			for (int i = 0; i <5 ; i++) 
			{ 
				System.out.print((char)fileReader.read()); 
			} 
			fileReader.close(); 
		}
		catch(Exception e)
		{
			System.out.print("Error: "+e.toString());
		}
	} 
}


HelloHello

Example 1: Reset FilterReader in Java

If we call the mark() and reset() method one after another then apparently they will not show any effect just because of the recently marked position and the current reading positions are the same. In such a cases it will continue to read from the current position.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Hello Studytonight"); 
			FilterReader fileReader = new FilterReader(reader){};
			for (int i = 0; i <5 ; i++) 
			{ 
				System.out.print((char)fileReader.read()); 
			} 
			if(fileReader.markSupported()) 
			{ 
				fileReader.mark(100); 
			} 
			fileReader.reset(); 
			for (int i = 0; i <14; i++) 
			{ 
				System.out.print((char)fileReader.read()); 
			} 
			fileReader.close(); 
		}
		catch(Exception e)
		{
			System.out.print("Error: "+e.toString());
		}
	} 
}


Hello Studytonight

Conclusion

In this tutorial, we learned about the reset() method of the FilterReader class in Java, which is used to reset the stream to the point where the mark() method was most recently invoked.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.