PUBLISHED ON: MARCH 8, 2021
Java InputStreamReader ready() method
In this tutorial, we will learn about the ready()
method of the InputStreamReader class in Java. This method verifies whether the current stream is ready to be read by the Input Stream reader or not. The InputStreamReader is ready if bytes are available to be read from the underlying byte stream or if the input buffer is not empty.
Syntax
Here is the syntax declaration of this method. No parameters are accepted. It returns true
if the stream is ready to be read, otherwise, false
.
public boolean ready()
Example 1: Ready Method of InputStreamReader
In this example, we will illustrate how to check whether the current stream is ready to be read by the Input Stream reader or not. The InputStreamReader is ready if bytes are available to be read from the underlying byte stream or if the input buffer is not empty.
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StudyTonight
{
public static void main(String args[])
{
try
{
InputStream inputStream = new FileInputStream("E://studytonight//output.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
boolean status = inputStreamReader.ready();
System.out.println("inputStreamReader.ready(): " + status);
}
catch (Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
inputStreamReader.ready(): true
output.txt
Hello Studytonight
Example 1: Ready Method of InputStreamReader
In the following example, we are reading the data from the file using the read method, and while the data we also print the value returned by the ready() method, each time this method will return the true value because the stream is ready to read the next value except once all the character reading is finished. After that, it will return the false value because the reading is done.
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class StudyTonight
{
public static void main(String args[])
{
boolean bool = false;
int i;
char c;
try
{
FileInputStream fileInputStream = new FileInputStream("E://studytonight//output.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
while((i = inputStreamReader.read())!=-1)
{
c = (char)i;
System.out.println("Character read: "+c);
bool = inputStreamReader.ready();
System.out.println("Ready to read: "+bool);
}
}
catch (Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Character read: H
Ready to read: true
Character read: e
Ready to read: true
Character read: l
Ready to read: true
Character read: l
Ready to read: true
Character read: o
Ready to read: true
Character read:
Ready to read: true
Character read: S
Ready to read: true
Character read: t
Ready to read: true
Character read: u
Ready to read: true
Character read: d
Ready to read: true
Character read: y
Ready to read: true
Character read: t
Ready to read: true
Character read: o
Ready to read: true
Character read: n
Ready to read: true
Character read: i
Ready to read: true
Character read: g
Ready to read: true
Character read: h
Ready to read: true
Character read: t
Ready to read: false
output.txt
Hello Studytonight
Conclusion
In this tutorial, we learned about the ready()
method of the InputStreamReader class in Java, which tells if the current stream is ready to be read by the Input Stream Reader. It returns true
, if the stream is ready, else, it returns false
.