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

FilterReader ready() Method in Java

In this tutorial, we will learn about the ready() method of FilterReader class in Java. This method is used to check whether this stream is ready to be read or not. It is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

Syntax

This is the syntax declaration of a ready() method, this method does not accept any parameter and returns the boolean value based on whether the stream is ready to read or not.

public boolean ready();

Example 1: Ready Method of FilterReader

In this example, we are calling the ready() method to check whether the stream is ready to read the data from it or not, here this method is returning the true value it means this method is ready to read.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Welcome to Studytonight"); 
			FilterReader filterReader = new FilterReader(reader){}; 
			
			boolean isReady = filterReader.ready(); 
			System.out.println("Stream is ready to read:"+isReady);
			filterReader.close(); 
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Stream is ready to read:true

Example 2: Ready Method of FilterReader

In this example, we are implementing various methods of FIlterReader class. Here, initially, we created a reader using StringReader class. Then by using the skip() method of FilterReader class, it will skip reading the first 8 characters and it will point to the 9th character and that's why when we are trying to read then it will start from the 9th character. We can use the ready() method to check whether the reader is ready to read or not. In the program, we are reading eight characters ahead from the 9th position and then after calling the reset() method pointer will come back to the initial position. We can see when we are reading the data after the reset method it is showing from the beginning.

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			Reader reader = new StringReader("Welcome to Studytonight"); 
			FilterReader filterReader = new FilterReader(reader){}; 
			char ch[] = new char[28]; 		
			filterReader.skip(8); 
			if(filterReader.ready()) 
			{ 
				filterReader.read(ch); 
				for (int i = 0; i < 8; i++)  
				{ 
					System.out.print(ch[i]); 
				} 
				System.out.print("\n");		

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

to Study

Welcome

Conclusion

In this tutorial we learned about ready() method, This method is used to check whether this stream is ready to be read or not. It is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.