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.

java_io_reader.htm
Advertisements