例子
以下示例显示java.io.BufferedReader.ready()方法的用法。
package com.jc2182;
import java.io.BufferedReader;
import java.io.StringReader;
import java.nio.CharBuffer;
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringReader sr = null;
BufferedReader br = null;
try {
sr = new StringReader(s);
// create new buffered reader
br = new BufferedReader(sr);
// Destination source is created
CharBuffer target = CharBuffer.allocate(s.length());
// ready is invoked to test if character stream is ready
if(br.ready()) {
br.read(target);
}
System.out.print(target.array());
} catch(Exception e) {
e.printStackTrace();
} finally {
// releases resources associated with the streams
if(br!=null)
br.close();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
ABCDEFGHIJKLMNOPQRSTUVWXYZ