Signup/Sign In
PUBLISHED ON: FEBRUARY 24, 2021

Java CharArrayReader Class

In this tutorial, we will learn about CharArrayReader class in Java. This class creates a character buffer using a character array. This class provides various methods like mark(), read(), close(), reset(), markSupported(), skip() and ready()

Syntax

This is the syntax declaration of CharArrayReader class in Java. We can see this class inherits Reader class.

public class CharArrayReader extends Reader  

Java CharArrayReader class Methods

All the method of CharAraayReader class is given in the table below

Method Description
int read() This method is used to read a single character
int read(char[] b, int off, int len) This method is used to read characters into the portion of an array.
boolean ready() This method is used to tell whether the stream is ready to read.
boolean markSupported() This method is used to tell whether the stream supports mark() operation.
long skip(long n) This method is used to skip the character in the input stream.
void mark(int readAheadLimit) This method is used to mark the present position in the stream.
void reset() This method is used to reset the stream to a most recent mark.
void close() This method is used to closes the stream.

Example of CharArrayReader Class

In the following example, we are implementing the read() method using CharArrayReader class. We read a character from the stream one by one until it reaches the end of the stream, the end of the stream will be -1.

package studytonight;
import java.io.CharArrayReader;
public class StudyTonight 
{
	public static void main(String args[]) throws Exception
	{
		char[] arr = { 's', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't' };  
		CharArrayReader reader = new CharArrayReader(arr);  
		int ch = 0;  
		while ((ch = reader.read()) != -1) {  
			System.out.print((char)ch);  
		}  
	}
}


studytonight

Example of CharArrayReader Class

In the following example, we will implement mark(), reset() and markSupported() methods here read method will read a character and print it on screen. Then using the mark() method we mark the point at the particular position and using markSupported() method we check whether our position is marked or not and using reset method stream will reposition where we marked the position and again it will provide value from that point.

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

		System.out.println("Element : "+(char)reader.read()); 
		System.out.println("Element : "+(char)reader.read()); 
		System.out.println("Element : "+(char)reader.read()); 
		reader.mark(0);          
		System.out.println("Element : "+(char)reader.read()); 
		System.out.println("Element : "+(char)reader.read());       

		if (reader.markSupported()) 
		{ 
			System.out.println("\nmark() is supported\n"); 
			reader.reset(); 
			System.out.println("Element : "+(char)reader.read()); 
			System.out.println("Element : "+(char)reader.read()); 
		} 
		else
		{
			System.out.println("mark() method not supported."); 
		}	
	}
}


Element : s
Element : t
Element : u
Element : d
Element : y

mark() is supported

Element : d
Element : y

Conclusion

In this tutorial, we learned about CharArrayReader class in Java. CharArrayReader class creates a character buffer using a character array.

Using the mark() method we can set the position from where we can start reading whenever it is required. Later we can start reading the data from that point only.



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.