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

FilterReader mark() Method in Java

In this tutorial, we will learn about mark() method of FilterReader class in Java. This method marks the present position in the stream, when we call the reset() method at that time we can retrieve the recently marked position by the mark() method and we can start reading from that position.

Syntax

This is the syntax declaration of the mark() method, this method does not return anything since it's return type is void but it takes a parameter as readAheadLimit, basically, it limits the number of characters that may be read while still preserving the mark.

public void mark(int readAheadLimit)

Example 1: Mark Method of FilterReader

In this example, we will demonstrate the use of the mark() method, we call that method to mark the position at buffer, when we call the reset method then it will go to the most recently marked position and from that point, we can read the data further.

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		FilterReader fr = null;
		Reader r = null;      

		try 
		{
			r = new StringReader("ABCDEF");

			fr = new FilterReader(r) {};

			System.out.println((char)fr.read());
			System.out.println((char)fr.read());

			fr.mark(0);
			System.out.println("mark() called");
			System.out.println((char)fr.read());
			System.out.println((char)fr.read());

			fr.reset();
			System.out.println("reset() called");
			System.out.println((char)fr.read());
			System.out.println((char)fr.read());

		} 
		catch(IOException e)
		{
			System.out.println("Error: "+e.toString());
		}  
	}
}


A
B
mark() called
C
D
reset() called
C
D

Example 2: Mark Method of FilterReader

Here, we are implementing the mark() method but we will not use the reset() method to show how these both methods work together, when we call to mark() method it marks the position at buffer and starts reading the data if we do not call reset() method then apparently it will not show any difference in working of the read() method.

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		FilterReader fr = null;
		Reader r = null;      
		try 
		{
			r = new StringReader("ABCDEF");
			fr = new FilterReader(r) {};
			System.out.println((char)fr.read());
			System.out.println((char)fr.read());
			fr.mark(0);
			System.out.println("mark() called");
			System.out.println((char)fr.read());
			System.out.println((char)fr.read());
			System.out.println((char)fr.read());
			System.out.println((char)fr.read());
		} 
		catch(IOException e)
		{
			System.out.println("Error: "+e.toString());
		}  
	}
}


A
B
mark() called
C
D
E
F

Conclusion

In this tutorial, we learned about the mark() method of FilterReader class in Java. This method marks the present position in the stream, when we call the reset() method at that time we can retrieve the recently marked position by the mark() method and we can start reading from that position.