Java StringReader.ready() example

In this java tutorial, You will learn how to use ready() method of StringReader class.The ready() method tells whether this stream is ready to be read.

Tells whether this stream is ready to be read.

syntax

public boolean ready() throws IOException

Returns

True if the next read() is guaranteed not to block for input.

Throws

IOException - If the stream is closed.

Java StringReader ready() example

  • Create String instance String str = "Technicalkeeda!";
  • Create StringReader instance new StringReader(str);
  • stringReader.ready(); Check whether this stream is ready to be read.
  • (char) stringReader.read() Print each character from the reader.
  • stringReader.close(); Close the reader.
package com.technicalkeeda;

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

public class App {

    public static void main(String[] args) {
        try {
            String str = "Technicalkeeda!";

            StringReader stringReader = new StringReader(str);

            System.out.println(stringReader.ready());

            for (int i = 0; i < str.length(); i++) {
                System.out.print((char) stringReader.read());
            }

            stringReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

output

true
Technicalkeeda!