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

Java BufferedReader mark() Method

In this tutorial, we will learn about the mark() method of the BufferReader class in Java. This method marks the current position in the buffer reader stream. And once this method is called, the reset() method of the same BufferedReader class is called subsequently, which resets the stream to the most recent point.

Syntax

Below is the syntax of this method, the parameter of which is readAheadLimit, which represents the maximum number of characters that can be read before invalidating the mark. The return type of this method is void.

public void mark(int readAheadLimit) throws IOException  

Example 1: BufferedReader mark() Method

In this example, we are implementing the mark() method, firstly we read the data from the BufferedReader and then called the mark() a method so that we can retrieve the data later from this position, again we start reading the data. When we call the reset() method it will set the position of the stream at the recently marked position by mark() method and we will start reading data from that position.

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 mark() Method

In this example, we are marking the positions at the stream by calling the mark() method so that we can start reading the data from these positions whenever required, here firstly we read the data and mark() position. When we call the reset() method it will set the position of the stream at the recently marked position by mark() method and we will start reading data from that position.

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 the mark() method of the BufferedReader class in Java, which marks the current position in the buffer reader stream, and the reset() method of the same class fixes the stream to this point.