BufferedReader 未声明“就绪”当它应该
我正在尝试使用 BufferedReader 通过 URL 上的 InputStreamReader(到某些 Apache 服务器上的文件)从 Web 文档中读取文本。
String result = "";
URL url = new URL("http://someserver.domain/somefile");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(url.openStream(), "iso-8859-1"));
result += in.readLine();
现在这工作得很好。但显然我希望读者不要只阅读一行,而是尽可能多地阅读文件中的内容。
查看 BufferedReader API,下面的代码应该做到这一点:
while (in.ready()) {
result += in.readLine();
}
即,当有更多行时读取所有行,当没有更多行时停止。然而,这段代码不起作用 - 读者只是从不报告ready() = true!
我什至可以在读取一行(从文件中读取正确的字符串)之前打印 read() 值,但读取器会报告“false”。
我做错了什么吗?为什么 BufferedReader 在实际有内容需要读取时在就绪时返回“false”?
I am trying to read text from a web document using a BufferedReader over an InputStreamReader on an URL (to the file on some Apache server).
String result = "";
URL url = new URL("http://someserver.domain/somefile");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(url.openStream(), "iso-8859-1"));
result += in.readLine();
Now this works just fine. But Obviously I'd like the reader not to just read one line, but as many as there are in the file.
Looking at the BufferedReader API the following code should do just that:
while (in.ready()) {
result += in.readLine();
}
I.e. read all lines while there are more lines, stop when no more lines are there. This code does not work however - the reader just never reports ready() = true!
I can even print the ready() value right before reading a line (which reads the correct string from the file) but the reader will report 'false'.
Am I doing something wrong? Why does the BufferedReader return 'false' on ready when there is actually stuff to read?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
ready() != has more
ready()
并不表示还有更多数据需要读取。它仅显示读取will是否会阻塞线程。在读取所有数据之前,它很可能会返回 false。要确定是否没有更多数据,请检查
readLine()
是否返回null
。ready() != has more
ready()
does not indicate that there is more data to be read. It only shows if a readwillcould block the thread. It is likely that it will return false before you read all data.To find out if there is no more data check if
readLine()
returnsnull
.另一种绕过
in.ready()
的方法是:您将继续阅读,直到完成。这样你就不需要担心
in.ready()
的问题。Another way you can do this that bypasses the
in.ready()
is something like:You will just continue reading until you are done. This way you do not need to worry about the problem with
in.ready()
.我认为编写此代码的标准方法是尝试读取该行并验证它是否在某个时候返回。像这样的事情:
所以你只要继续下去,直到从流中返回 null 为止。有关更多信息,请参阅此处:
http ://download.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine()
I think the standard way to write this is to just attempt to read the line and verify that it returned sometime. Something like this:
So you just continue to go until you get null returned from the stream. See here for extra information:
http://download.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine()
BufferedReader.ready()
方法的行为符合指定:Reader.ready()
javadoc 表示以下内容:然后是
BufferedReader。 read()
javadoc 的内容如下:如果将这两者放在一起,很明显,在字符可用的情况下,
BufferedReader.ready()
可以返回false
。简而言之,您不应该依赖ready()
来测试逻辑文件结束或流结束。The
BufferedReader.ready()
method is behaving as specified:The
Reader.ready()
javadoc says the following:Then the
BufferedReader.ready()
javadoc says the following:If you put these two together, it is clear that
BufferedReader.ready()
can returnfalse
in situations where are characters available. In short, you shouldn't rely onready()
to test for logical end-of-file or end-of-stream.这是我们多年来一直使用的方法 - 不确定这是否是“标准”方法。我想听听有关直接使用 URL.openURLStream() 的优缺点的评论,以及这是否会导致 OP 的问题。此代码适用于 HTTP 和 HTTPS 连接。
This is what we have been using consistently for years - not sure if it is the "standard" method. I'd like to hear comments about the pros and cons of using URL.openURLStream() directly, and if that is causing the OP's problems. This code works for both HTTP and HTTPS connections.
基本上,BufferedReader.ready() 方法可用于检查底层流是否准备好向方法调用者提供数据......否则我们可以等待线程一段时间,直到它准备好。
但真正的问题是,当我们完全读取数据流后,它会抛出 false..
所以我们不知道流是否已完全读取或底层流正忙......
Basically the BufferedReader.ready() method can be used for checking whether the underlying stream is ready for providing data to the method caller.... else we can wait the thread for some time till it becomes ready.
But the real problem is that after we completely read the data stream, it will throw false..
so we didn't know whether the stream is fully read OR underlying stream is busy....
如果您想使用 in.ready(),以下内容对我来说效果很好:
If you want to use in.ready(), the following worked for me well: