BufferedReader.ready() 方法是否确保 readLine() 方法不返回 NULL? [英] Does BufferedReader.ready() method ensure that readLine() method does not return NULL?

查看:44
本文介绍了BufferedReader.ready() 方法是否确保 readLine() 方法不返回 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码可以使用 BufferedReader 读取文本文件:

I have such code to read a text file using BufferedReader:

BufferedReader reader=null;
    try {
        reader = new BufferedReader(new FileReader("file1.txt"));

        while (reader.ready()) {
            final String line = reader.readLine();
            System.out.println("<"+line+">");
        } catch (..)
    {
        ...
    }

它工作正常,但 Findbugs 报告警告:

It works correctly but Findbugs reports a warning:

NP_DEREFERENCE_OF_READLINE_VALUE :调用 readLine() 的结果是取消引用而不检查查看如果结果为空.如果没有要阅读的文本行数更多,readLine()将返回 null 并取消引用这将产生一个空指针例外.

NP_DEREFERENCE_OF_READLINE_VALUE : The result of invoking readLine() is dereferenced without checking to see if the result is null. If there are no more lines of text to read, readLine() will return null and dereferencing that will generate a null pointer exception.

当我将 FileReader 更改为 StringReader 时,即

When I change FileReader to StringReader, i.e.

BufferedReader reader=null;
    try {
        reader = new BufferedReader(new StringReader("ABCD"));

        while (reader.ready()) {
            final String line = reader.readLine();
            System.out.println("<"+line+">");
        } catch (..)
    {
        ...
    }

readLine 方法返回 nullready 方法总是返回 true - 实际上这是一个无限循环.

the readLine method returns null while the ready method always returns true - indeed this is an infinite loop.

这似乎 readLine 可能会返回 null,即使 ready 返回 true.但是为什么不同的 Reader 的行为会有所不同?

This seems that the readLine may return null even if ready returns true. But why does the behavior differ for different Readers?

更新:

我确实知道阅读文本文件的正常方式(就像彼得和阿里所说明的那样).但我读了同事的那段代码,意识到我不知道 ready 方法.然后我阅读了 JavaDoc,但不理解 block.然后我做了一个测试并发布了这个问题.因此,提出这个问题的更好方法可能是:

I do know the normal way to read a text file (just like Peter and Ali illustrated). but I read that piece of code from my colleague and realized that I don't know the ready method. Then I read the JavaDoc, but don't understand block. Then I did a test and posted this question. So, the better way to put this question might be:

输入何时会阻塞?如何使用 ready 方法(或者为什么不使用它)?为什么这两个 Reader(FileReaderStringReader)在 ready 方法方面表现不同?>

When will the input be blocking? How to use the ready method (or why not to use it)? Why do those 2 Readers (FileReader and StringReader) behave differently with regards to the ready method?

推荐答案

ready 方法告诉我们 Stream 是否准备好被读取.

The ready method tells us if the Stream is ready to be read.

假设您的流正在从网络套接字读取数据.在这种情况下,流可能还没有结束,因为套接字还没有关闭,但它可能还没有准备好接收下一个数据块,因为套接字的另一端没有推送更多数据.

Imagine your stream is reading data from a network socket. In this case, the stream may not have ended, because the socket has not been closed, yet it may not be ready for the next chunk of data, because the other end of the socket has not pushed any more data.

在上面的场景中,我们无法读取更多数据,直到远程端推送它,因此我们必须等待数据变为可用,或者等待套接字关闭.ready() 方法告诉我们数据何时可用.

In the above scenario, we cannot read any more data until the remote end pushes it, so we have to wait for the data to become available, or for the socket to be closed. The ready() method tells us when the data is available.

这篇关于BufferedReader.ready() 方法是否确保 readLine() 方法不返回 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆