Java中的 PushbackReader ready() 方法和示例
Java中PushbackReader类的ready()方法用于检查这个PushbackReader是否准备好被读取。它返回一个布尔值,说明阅读器是否准备好。
句法:
public void ready()
参数:此方法不接受任何参数
返回值:此方法返回一个布尔值,该值指示此 PushbackReader 是否已准备好读取。如果准备好,则返回 true。否则返回false。
异常:如果输入输出时发生错误,此方法将抛出IOException 。
下面的方法说明了 ready() 方法的工作:
方案一:
// Java program to demonstrate
// PushbackReader ready() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
// Initializing a StringReader
// and PushbackReader
String s = "GeeksForGeeks";
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
// Check if the PushbackReader is
// ready to be read using ready()
System.out.println("Is PushbackReader ready "
+ "to be read: "
+ pushbackReader.ready());
// Close the stream using ready()
pushbackReader.close();
System.out.println("Stream Closed.");
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
Is PushbackReader ready to be read: true
Stream Closed.
方案二:
// Java program to demonstrate
// PushbackReader ready() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
// Initializing a StringReader
// and PushbackReader
String s = "GFG";
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
// Check if the PushbackReader is
// ready to be read using ready()
System.out.println("Is PushbackReader ready "
+ "to be read: "
+ pushbackReader.ready());
// Close the stream
pushbackReader.close();
System.out.println("Stream Closed.");
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
Is PushbackReader ready to be read: true
Stream Closed.
参考: https://docs.oracle.com/javase/9/docs/api/ Java/io/PushbackReader.html#ready–