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

Java CharArrayReader ready() Method

In this tutorial, we will learn about the ready() method of the CharArrayReader class in Java. This method checks, if the stream is ready to be read or not, and as it is known that, char array readers are always ready to be read.

Syntax

No parameter is needed in this method and it returns a boolean value, true if the stream is ready to be read, else, it returns false.

public boolean ready() throws IOException 

Example 1: Ready Method of CharArrayReader

In this example, we will illustrate how the ready() method works, generally, we call this method before reading the data from the stream because this method returns the true if the stream is ready to read otherwise it returns a false value.

import java.io.CharArrayReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try 
		{
			char[] ch = { 'A', 'B', 'C', 'D', 'E' };

			CharArrayReader charArrayReader = new CharArrayReader(ch);

			boolean isReady = charArrayReader.ready();

			System.out.println("CharArrayReader is ready -> " + isReady);
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


CharArrayReader is ready -> true

Example 2: Ready Method of CharArrayReader

Here, we are implementing the ready() method to check if the stream is ready to read the data from the stream, we will read from the stream only after the stream is ready to read the data.

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		String s = "studytonight.com";  
		Reader reader = new StringReader(s);  
		try 
		{  
			if(reader.ready())
			{
				for (int i = 0; i < 12; i++)
				{  
					char c = (char) reader.read();  
					System.out.print("" + c);  
				}  
			}
			reader.close();  
		} 
		catch (IOException e) 
		{  
			System.out.println("Error: "+e.toString());  
		}  
	} 
}

studytonight

Conclusion

In this tutorial, we learned about the ready() method of the CharArrayReader class in Java, which is used to validate if the stream is ready to be read or not, and returns a boolean value, true, if it ready, otherwise, false.