
- 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 - InputStreamReader ready() method
Description
The Java InputStreamReader ready() method checks whether the stream is ready to be read without blocking. Returns true if the stream has data available for reading. 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.InputStreamReader.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 InputStreamReader ready() method
The following example shows the usage of Java InputStreamReader ready() method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) throws IOException { FileInputStream fis = null; InputStreamReader isr = null; boolean bool = false; int i; char c; try { // new input stream reader is created fis = new FileInputStream("test.txt"); isr = new InputStreamReader(fis); // reads into the char buffer while((i = isr.read())!=-1) { // converts int to char c = (char)i; // prints the character System.out.println("Character read: "+c); // true if the next read is guaranteed bool = isr.ready(); // prints System.out.println("Ready to read: "+bool); } } catch (Exception e) { // print error e.printStackTrace(); } finally { // closes the stream and releases resources associated if(fis!=null) fis.close(); if(isr!=null) isr.close(); } } }
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
Character read: A Ready to read: true Character read: B Ready to read: true Character read: C Ready to read: true Character read: D Ready to read: true Character read: E Ready to read: false
Example - Checking ready() Before Reading Using InputStreamReader
The following example shows the usage of Java InputStreamReader ready() method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) { if (reader.ready()) { // Check if the stream is ready System.out.println("Stream is ready. Reading data..."); int data; while ((data = reader.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 InputStreamReader, which supports ready().
Calls reader.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 BufferedReader for Efficient Reading
The following example shows the usage of Java InputStreamReader ready() method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("example.txt")))) { System.out.println("Checking if the stream is ready..."); if (bufferedReader.ready()) { // Check if input stream has data System.out.println("Stream is ready. Reading first line..."); String line = bufferedReader.readLine(); System.out.println("First line: " + line); } else { System.out.println("Stream is not ready."); } } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "Java Programming")
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 line... First line: Java Programming
Explanation
Uses BufferedReader, which extends InputStreamReader.
Calls bufferedReader.ready() to check if data is available.
If ready() returns true, it reads and prints the first line.
If ready() returns false, it prints "Stream is not ready.".