Java BufferedReader ready()方法

java.io.BufferedReader.ready() 用于测试输入流是否已准备好被读取。只有当缓冲区不为空或基础流准备好时,缓冲的字符流才准备就绪。

1 语法

public boolean ready()

2 参数

3 返回值

如果准备好读取流,则该方法返回true。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.BufferedReader.ready()方法的例子
 */
import java.io.BufferedReader;
import java.io.StringReader;
import java.nio.CharBuffer;

public class Demo {
    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

热门文章

优秀文章