java reader类 实例_Java Reader ready()用法及代码示例
Java中Reader類的ready()方法用于檢查此Reader是否已準(zhǔn)備好被讀取。它返回一個(gè)布爾值,該值指示閱讀器是否準(zhǔn)備就緒。
用法:
public void ready()
參數(shù):此方法不接受任何參數(shù)
返回值:此方法返回一個(gè)布爾值,該值指示此Reader是否已準(zhǔn)備好被讀取。如果準(zhǔn)備就緒,則返回true。否則返回false。
異常:如果輸入輸出時(shí)發(fā)生某些錯(cuò)誤,則此方法將引發(fā)IOException。
下面的方法說明了ready()方法的用法方式:
程序1:
// Java program to demonstrate
// Reader ready() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String str = "GeeksForGeeks";
// Create a Reader instance
Reader reader
= new StringReader(str);
// Check if the Reader is
// ready to be read using ready()
System.out.println("Is Reader ready "
+ "to be read: "
+ reader.ready());
// Get the character
// to be read from the stream
int ch;
// Read the first 5 characters
// to this reader using read() method
// This will put the str in the stream
// till it is read by the reader
for (int i = 0; i < 5; i++) {
ch = reader.read();
System.out.println("\nInteger value "
+ "of character read: "
+ ch);
System.out.println("Actual "
+ "character read: "
+ (char)ch);
}
reader.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
Is Reader ready to be read: true
Integer value of character read: 71
Actual character read: G
Integer value of character read: 101
Actual character read: e
Integer value of character read: 101
Actual character read: e
Integer value of character read: 107
Actual character read: k
Integer value of character read: 115
Actual character read: s
程序2:
// Java program to demonstrate
// Reader ready() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String str = "GeeksForGeeks";
// Create a Reader instance
Reader reader
= new StringReader(str);
reader.close();
// Check if the Reader is
// ready to be read using ready()
System.out.println("Is Reader ready "
+ "to be read: "
+ reader.ready());
// Get the character
// to be read from the stream
int ch;
// Read the first character
// to this reader using read() method
// This will put the str in the stream
// till it is read by the reader
ch = reader.read();
System.out.println("\nInteger value "
+ "of character read: "
+ ch);
System.out.println("Actual "
+ "character read: "
+ (char)ch);
reader.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
java.io.IOException: Stream closed
總結(jié)
以上是生活随笔為你收集整理的java reader类 实例_Java Reader ready()用法及代码示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 发送邮件昵称_javaMail
- 下一篇: java cmd找不到文件_cmd中输入