Signup/Sign In

Java BufferedReader reset() method

In this tutorial, we will learn about, reset() method of the BufferedReader class in Java. The work of this method is to reset the stream to the most recently marked position, which makes the same byte readable again.

Syntax

Below is the syntax of this method, no parameters are needed in this method, and it does not return any value.

public void reset() throws IOException  

Example 1: BufferedReader reset() Method

In this example, we read the line from the stream using the readLine() method and then mark position at that point after that we call the reset() method so it will reset the position of stream to the recently marked position and will read the data from that point again.

import java.io.BufferedReader;
import java.io.FileReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try 
		{
			FileReader	fileReader = new FileReader("E://studytonight//output.txt"); 
			BufferedReader buffReader = new BufferedReader(fileReader);
			
			System.out.println(buffReader.readLine());  
			buffReader.mark(1);  
			System.out.println(buffReader.readLine());  
			buffReader.reset();  
			System.out.println(buffReader.readLine());  
			
			fileReader.close();
			buffReader.close();			
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


Hello studytonight
Welcome to studytonight
Welcome to studytonight

output.txt

Hello studytonight
Welcome to studytonight

Example 2: BufferedReader reset() Method

Here we are implementing the reset() method, we can see in the code given below that firstly we read the line and mark the position by calling the mark() method then again we read the two lines using the readLine() method and after that call reset() method, it will mark the position of the stream at recently marked position and it will start reading from that point.

import java.io.BufferedReader;
import java.io.FileReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try 
		{
			FileReader	fileReader = new FileReader("E://studytonight//output.txt"); 
			BufferedReader buffReader = new BufferedReader(fileReader);

			System.out.println(buffReader.readLine());  
			buffReader.mark(1);  
			System.out.println(buffReader.readLine());  
			System.out.println(buffReader.readLine());  
			System.out.println(buffReader.readLine());  
			System.out.println(buffReader.readLine());  
			buffReader.reset();
			System.out.println("After Calling the reset() method");
			System.out.println(buffReader.readLine());  
			System.out.println(buffReader.readLine());  
			System.out.println(buffReader.readLine());  
			fileReader.close();
			buffReader.close();			
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


1
2
3
4
5
After Calling the reset() method
2
3
4

output.txt

1
2
3
4
5
6
7
8
9

Conclusion

In this tutorial, we learned about reset() method of the BufferedReader class in Java, which resets the stream to the most recent mark, and throws an exception if the value of the mark has become invalid or it has never been marked previously on the stream.



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.