java.io.BufferedReader.ready() 方法通知流是否已准备好被读取。一个缓冲字符流是只准备当缓冲区不为空,或者底层流已准备就绪。更多教程请访问http://www.manongzj.com
声明
以下是java.io.BufferedReader.ready()方法的声明
public boolean ready()
参数
-
NA
返回值
如果流是随时可以读取该方法返回true。
异常
-
IOException -- if an I/O error occurs
例子
下面的示例演示java.io.BufferedReader.ready()方法的用法。
package com.yiibai; 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