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

Java CharArrayReader mark() Method

In this tutorial, we will learn about the mark() method of CharArrayReader class in Java. The work of this method is to mark the position of the point, from where the reading of the stream will be started, and calling of reset() will again bring the stream to the point positioned by the mark() method.

Syntax

This is the syntax of this method, in which the maximum number of characters that can be read while the mark is valid, is passed as a parameter. No value is returned by this method.

public void mark(int readAheadLimit)

Example 1: Mark Position using CharArrayReader

In this example, we will illustrate how to use the mark() method to set the position mark in the stream, here firstly we read the data from the character array and then called the mark() and again read the data but when we called reset() method it enables the position to recently marked position so it will start read from that point again.

import java.io.CharArrayReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try 
		{
			char[] arr = {'s', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'}; 
			char c;
			CharArrayReader reader = new CharArrayReader(arr); 
			c=(char)reader.read();
			System.out.println(c);  
			reader.mark(1);  
			c=(char)reader.read();
			System.out.println(c);  
			reader.reset();  
			c=(char)reader.read();
			System.out.println(c);  

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


s
t
t

Example 2: Mark Position using CharArrayReader

Here we are implementing the one interesting case with the mark() method we call mark() and reset() method one after another and the reset() method will the position of stream to the last marked position and here last marked position is just recent one so apparently it will not show any difference in the output.

import java.io.CharArrayReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try { 
            char[] arr = {'S', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'}; 
  
            CharArrayReader reader = new CharArrayReader(arr);   

            int ch; 
  
            for (int i = 0; i < 5; i++) { 
                ch = reader.read(); 
                System.out.print((char)ch); 
            } 
  
            System.out.println(); 
  
            reader.mark(1); 
  
            reader.reset(); 

            for (int i = 0; i < 7; i++) 
            { 
                ch = reader.read(); 
                System.out.print((char)ch); 
            } 
        } 
        catch (Exception e)
		{ 
            System.out.println("Error: "+e.toString()); 
        } 
	} 
}


Study
tonight

Conclusion

In this tutorial, we learned about the mark() method of CharArrayReader class in Java, which is used to specify the position from which the stream read will be started if the reset() method is called in the stream.