Signup/Sign In

Java PipedReader ready() Method

In this tutorial, we will learn about the ready() method of the PipedReader class in Java. This method verifies whether this PipedReader stream is ready to be read or not. A piped character stream is ready if the circular buffer is not empty. It is a non-non-static method, available in the java.io package, which must be accessed only with class objects, trying to access it with the class name will throw an IOException.

Syntax

This is the syntax declaration of this method. It does not accept any parameter and it returns true if the stream is ready to be read, otherwise it returns false.

public boolean ready()

Example: Check Whether the PipedReader is ready

In the following example, we are checking if the given pipedReader is ready to read or not, here this method is returning the true value indicates that the given method is read to read.

import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PipedWriter pipedWriter=new PipedWriter();  
			PipedReader pipedReader=new PipedReader(pipedWriter);  
			pipedWriter.write("Hello Studytonight");  
			System.out.println("PipedReader is ready: "+pipedReader.ready());
			pipedReader.close();  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


PipedReader is ready: true

Example 2: Check Whether the PipedReader is ready

In the following example, we are checking if the given pipedReader is ready to read or not, here this method is returning a false value, so we can clearly say the given PipedReader is not ready to read the data.

import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PipedWriter pipedWriter=new PipedWriter();  
			PipedReader pipedReader=new PipedReader(pipedWriter);  
			boolean status = pipedReader.ready();
			System.out.println("pipedReader.ready(): " + status);
			pipedReader.close();
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


pipedReader.ready(): false

Conclusion

In this tutorial, we learned about the ready() method of the PipedReader class in Java. which is used to check if this PipedReader stream is ready to be read or not. If it is ready, it returns, without blocking the stream for next read(), else, it returns false.



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.