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

Java BufferedReader ready() Method

In this tutorial, we will learn about the ready() method of BufferedReader class in Java. This method checks whether the buffer stream is ready to be read or not. The buffered character stream proceeds only in two cases, either the buffer is not empty, or when the mainstream is ready.

Syntax

This is the syntax of this method. No parameter is needed for this method, it returns true if the stream is ready to be read, otherwise, false.

public boolean ready() throws IOException  

Example 1: BufferedReader ready() Method

In this example, we are checking if the stream whether is ready to read or not. In this example when we call the ready() method it is printing the true it means the given stream is ready to read the data from it.

import java.io.BufferedReader;
import java.io.FileReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try 
		{
			FileReader	fileReader = new FileReader("E://studytonight//output.txt"); 
			BufferedReader br = new BufferedReader(fileReader);
			
			boolean ready = br.ready();
			
			System.out.println("Stream is ready to read: "+ready);  
			fileReader.close();
			br.close();
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


Stream is ready to read: true

Example 2: BufferedReader ready() Method

In this example, we are checking if the stream is ready to read and then we are reading the characters from a stream until the ready() method is giving output true. Once the ready() method returns the output false we stop reading the data from it.

import java.io.BufferedReader;
import java.io.FileReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try 
		{
			FileReader	fileReader = new FileReader("E://studytonight//output.txt"); 
			BufferedReader buffReader = new BufferedReader(fileReader);
			boolean ready = buffReader.ready();
			while (ready) 
			{ 
				System.out.print((char)buffReader.read()); 
				ready = buffReader.ready(); 
			} 
			fileReader.close();
			buffReader.close();			
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


Hello studytonight

Conclusion

In this tutorial, we learned about ready() method of BufferedReader class in Java, which confirms if the buffer stream is ready to be read or not and returns true or false depending upon the same.