Java.io.BufferedReader.ready() Method



Description

The java.io.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.

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

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

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();
      }
   }
}

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

ABCDEFGHIJKLMNOPQRSTUVWXYZ
java_io_bufferedreader.htm
Advertisements