Java StringReader.ready() example

Java StringReader.ready() example

By technicalkeeda, Views 291, Date 28-Feb-2018

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.

tags Java

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!
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]