
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - FilterReader ready() method
Description
The Java FilterReader ready() method checks if the stream is ready to be read without blocking. It helps in avoiding unnecessary delays when reading data. Returns false if no data is available or if the stream is at EOF (end of file). Prevents unnecessary read() calls when no data is available.
Declaration
Following is the declaration for java.io.FilterReader.ready() method −
public boolean ready()
Parameters
NA
Return Value
The method returns true if the next read() is guaranteed not to block for input, else false.
Exception
IOException − If an I/O error occurs.
Example - Usage of FilterReader ready() method
The following example shows the usage of Java FilterReader ready() method.
FilterReaderDemo.java
package com.tutorialspoint; import java.io.FilterReader; import java.io.Reader; import java.io.StringReader; public class FilterReaderDemo { public static void main(String[] args) throws Exception { FilterReader fr = null; Reader r = null; boolean bool = false; try { // create new reader r = new StringReader("ABCDEF"); // create new filter reader fr = new FilterReader(r) { }; // true if the filter reader is ready to be read bool = fr.ready(); // prints System.out.println("Ready to read? "+bool); } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases system resources associated with this stream if(r!=null) r.close(); if(fr!=null) fr.close(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Ready to read? true
Example - Checking if the Stream is Ready Before Reading Using BufferedReader
The following example shows the usage of Java FilterReader ready() method.
FilterReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FilterReaderDemo { public static void main(String[] args) { try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) { if (fr.ready()) { // Check if the stream is ready System.out.println("Stream is ready. Reading data..."); int data; while ((data = fr.read()) != -1) { // Read character by character System.out.print((char) data); } } else { System.out.println("Stream is not ready."); } } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "Hello")
Let us compile and run the above program, this will produce the following result−
Stream is ready. Reading data... Hello
Explanation
Uses BufferedReader, which supports ready().
Calls fr.ready() to check if the file has data available.
If ready() returns true, reads one character at a time.
If ready() returns false, it prints "Stream is not ready.".
Example - Using ready() with PushbackReader
The following example shows the usage of Java FilterReader ready() method.
FilterReaderDemo.java
package com.tutorialspoint; import java.io.FileReader; import java.io.FilterReader; import java.io.IOException; import java.io.PushbackReader; public class FilterReaderDemo { public static void main(String[] args) { try (FilterReader fr = new PushbackReader(new FileReader("example.txt"))) { System.out.println("Checking if the stream is ready..."); if (fr.ready()) { // Check if input stream has data System.out.println("Stream is ready. Reading first character..."); int data = fr.read(); System.out.println("First character: " + (char) data); } else { System.out.println("Stream is not ready."); } } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "Java")
Let us compile and run the above program, this will produce the following result−
Checking if the stream is ready... Stream is ready. Reading first character... First character: J
Explanation
Uses PushbackReader, another FilterReader subclass.
Calls fr.ready() to check if data is available.
If ready() returns true, it reads the first character and prints it.
Otherwise, it prints "Stream is not ready.".