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

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

问题描述

我有这样的代码来使用 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报告警告:
$ b


NP_DEREFERENCE_OF_READLINE_VALUE:
调用readLine()是
解除引用,如果结果为空,则不检查
。如果没有
多行文本可读,readLine()
将返回null并取消引用
,这将生成一个空指针
异常。


当我将 FileReader 更改为 StringReader ,即

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

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

readLine 方法返回 null 就绪方法总是返回 true - 实际上这是一个无限循环。

这似乎是<$ c即使准备好了,$ c> readLine 可能会返回 null 返回。但为什么不同的 Reader s?



UPDATE



我知道读取文本文件的正常方法(就像Peter和Ali说明的那样)。但是我从同事那里读了一段代码,发现我不知道 ready 方法。然后我读了JavaDoc,但不明白 block 。然后我做了一个测试并发布了这个问题。所以,更好的方法来提出这个问题可能是:

什么时候输入被阻塞?如何使用准备方法(或者为什么不使用它)?为什么那些2 Reader s( FileReader StringReader )对于准备方法的行为有所不同?

解决方案


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



在上面的场景中,我们不能读取更多的数据,直到远端推送它,所以我们必须等待数据可用,或者关闭套接字。 ready()方法告诉我们数据何时可用。

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 (..)
    {
        ...
    }

It works correctly but Findbugs reports a warning:

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.

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 (..)
    {
        ...
    }

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

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

UPDATE:

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:

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?

解决方案

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.

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天全站免登陆