
- 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 - Reader ready() method
Description
The Java Reader ready() method tells whether this stream is ready to be read.
Declaration
Following is the declaration for java.io.Reader.ready() method.
public boolean ready()
Parameters
NA
Return Value
This method returns true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Exception
IOException − if some I/O error occurs.
Example - Usage of Reader ready() method
The following example shows the usage of Reader ready() method.
ReaderDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class ReaderDemo { public static void main(String[] args) { String s = "Hello World"; // create a new StringReader Reader reader = new StringReader(s); try { // check if reader is ready System.out.println( reader.ready()); // read the first five chars for (int i = 0; i < 5; i++) { char c = (char) reader.read(); System.out.print( c); } // change line System.out.println(); // close the stream reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
true Hello
Example - Using ready() with StringReader
The following example shows the usage of Reader ready() method.
ReaderDemo.java
package com.tutorialspoint; import java.io.StringReader; import java.io.IOException; public class ReaderDemo { public static void main(String[] args) { try (StringReader reader = new StringReader("Hello World")) { if (reader.ready()) { System.out.println("Reader is ready to read."); int data = reader.read(); System.out.println("First character: " + (char) data); } else { System.out.println("Reader is not ready."); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Reader is ready to read. First character: H
Explanation
StringReader is in-memory and always ready, so ready() returns true.
We then read and print the first character.
Example - Using ready() with BufferedReader on System.in (keyboard input)
The following example shows the usage of Reader ready() method.
ReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class ReaderDemo { public static void main(String[] args) { System.out.println("Type something and press Enter:"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { while (!reader.ready()) { // Wait until input is ready } String input = reader.readLine(); System.out.println("You typed: " + input); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Type something and press Enter: [User types: Java] You typed: Java
Explanation
This program waits until the user types input.
ready() helps avoid blocking when checking if data is available.
Once the user types and presses Enter, readLine() reads the input.