PushbackReader ready() method in Java with Examples
Last Updated :
29 Apr, 2019
The ready() method of PushbackReader Class in Java is used to check whether this PushbackReader is ready to be read or not. It returns a boolean which states if the reader is ready.
Syntax:
public void ready()
Parameters: This method does not accepts any parameters
Return Value: This method returns a boolean value which tells if this PushbackReader is ready to be read or not. It return true if it is ready. Else it returns false.
Exception: This method throws IOException if some error occurs while input-output.
Below methods illustrates the working of ready() method:
Program 1:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String s = "GeeksForGeeks" ;
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
System.out.println( "Is PushbackReader ready "
+ "to be read: "
+ pushbackReader.ready());
pushbackReader.close();
System.out.println( "Stream Closed." );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
Is PushbackReader ready to be read: true
Stream Closed.
Program 2:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String s = "GFG" ;
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
System.out.println( "Is PushbackReader ready "
+ "to be read: "
+ pushbackReader.ready());
pushbackReader.close();
System.out.println( "Stream Closed." );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
Is PushbackReader ready to be read: true
Stream Closed.
Reference: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#ready–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...