Java.io.PushbackReader.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

