Java - BufferedReader ready() method



Description

The Java BufferedReader ready() method informs whether the stream is ready to be read. A buffered character stream is only ready when the buffer is not empty or if the underlying stream is ready.

ready() method returns true if the next read operation will not block, i.e., if there are characters available to read, or false otherwise.

Declaration

Following is the declaration for java.io.BufferedReader.ready() method.

public boolean ready()

Parameters

NA

Return Value

The method returns true if the stream is ready to be read.

Exception

IOException − If an I/O error occurs

Example - Using a BufferedReader ready() method

The following example shows the usage of BufferedReader ready() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.StringReader;
import java.nio.CharBuffer;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      StringReader sr = null;
      BufferedReader br = null;

      try {
         sr = new StringReader(s);   
         
         // create new buffered reader
         br = new BufferedReader(sr);
      
         // Destination source is created
         CharBuffer target = CharBuffer.allocate(s.length());
         
         // ready is invoked to test if character stream is ready
         if(br.ready()) {
            br.read(target);
         }
         System.out.print(target.array());
                           
      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         // releases resources associated with the streams
         if(br!=null)
            br.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Example - Using ready() to Check Before Reading

The following example shows the usage of BufferedReader ready() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      String input = "Line 1: Java\nLine 2: BufferedReader\nLine 3: ready() Example";

      // Initialize BufferedReader with a StringReader
      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         System.out.println("Reading lines using ready():");
         String line;
         // Loop while the stream is ready to be read
         while (reader.ready() && (line = reader.readLine()) != null) {
            System.out.println(line);
         }

         System.out.println("No more lines to read. BufferedReader is no longer ready.");
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The BufferedReader is ready to read.
Read line: Hello, World!

Explanation

  • A BufferedReader is created with a StringReader containing multiple lines of input.

  • The ready() method is used to check if the stream is ready for reading before performing the readLine() operation.

  • If ready() returns true, the first line is read and printed. Otherwise, an appropriate message is displayed.

  • This ensures that the program doesn't attempt to read from the stream if it is not ready.

Example - Using ready() in a Loop to Read Multiple Lines

The following example shows the usage of BufferedReader ready() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      String input = "Line 1: Java\nLine 2: BufferedReader\nLine 3: ready() Example";

      // Initialize BufferedReader with a StringReader
      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         System.out.println("Reading lines using ready():");
         String line;
         // Loop while the stream is ready to be read
         while (reader.ready() && line = br.readLine()) != null) {
            System.out.println(line);
         }

         System.out.println("No more lines to read. BufferedReader is no longer ready.");
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Reading lines using ready():
Line 1: Java
Line 2: BufferedReader
Line 3: ready() Example
No more lines to read. BufferedReader is no longer ready.

Explanation

  • The BufferedReader is created with a multi-line string as input.

  • A while loop is used to repeatedly check if the stream is ready using ready().

  • If ready() returns true, a line is read and printed using readLine().

  • The loop terminates when the stream is no longer ready (i.e., all input has been read).

  • This ensures that the program only reads from the stream when it has data available.

Key Points About ready()

  • Checks Readiness− The ready() method ensures that the stream is ready for reading, preventing blocking.

  • Efficient Usage− It is particularly useful in scenarios where you want to avoid blocking operations, such as reading from a slow or remote source.

  • Caveat− The ready() method does not guarantee that the stream will remain ready after the check, as conditions may change.

These examples demonstrate practical use cases for ready() to ensure non-blocking and efficient stream reading.

java_io_bufferedreader.htm
Advertisements