boolean ready


描述

该java.io.PushbackReader.ready()方法告诉此流是否已准备好被读取。

声明

以下是java.io.PushbackReader.ready()方法的声明。

public boolean ready()

参数

NA

返回值

如果保证下一个read()不阻止输入,则此方法返回true,否则返回false。请注意,返回false并不能保证下一次读取将被阻止。

异常

IOException - 如果发生I / O错误。

实例

以下示例显示了java.io.PushbackReader.ready()方法的用法。

package com.tutorialspoint;

import java.io.*;

public class PushbackReaderDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new StringReader
      StringReader sr = new StringReader(s);

      // create a new PushBack reader based on our string reader
      PushbackReader pr = new PushbackReader(sr, 20);

      try {
         // check if the reader is ready
         System.out.println("" + pr.ready());


         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.print("" + c);
         }

         // change line
         System.out.println();

         // close the stream
         pr.close();

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

让我们编译并运行上面的程序,这将产生以下结果

true
Hello