BufferedReader 未声明“就绪”当它应该

发布于 2024-10-14 04:54:51 字数 747 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情独悲 2024-10-21 04:54:51

ready() != has more

ready()并不表示还有更多数据需要读取。它仅显示读取 will 是否会阻塞线程。在读取所有数据之前,它很可能会返回 false。

要确定是否没有更多数据,请检查 readLine() 是否返回 null

String line = in.readLine();
while(line != null){
   ...
   line = in.readLine();
}

ready() != has more

ready() does not indicate that there is more data to be read. It only shows if a read will could 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() returns null.

String line = in.readLine();
while(line != null){
   ...
   line = in.readLine();
}
永言不败 2024-10-21 04:54:51

另一种绕过 in.ready() 的方法是:

while ((nextLine = in.readLine()) != null) {
  result += nextLine;
}

您将继续阅读,直到完成。这样你就不需要担心in.ready()的问题。

Another way you can do this that bypasses the in.ready() is something like:

while ((nextLine = in.readLine()) != null) {
  result += nextLine;
}

You will just continue reading until you are done. This way you do not need to worry about the problem with in.ready().

虐人心 2024-10-21 04:54:51

我认为编写此代码的标准方法是尝试读取该行并验证它是否在某个时候返回。像这样的事情:

while ((String nextLine = in.readLine()) != null) {
    //System.out.println(nextLine);
    result += nextLine;
}

所以你只要继续下去,直到从流中返回 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:

while ((String nextLine = in.readLine()) != null) {
    //System.out.println(nextLine);
    result += nextLine;
}

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()

挽你眉间 2024-10-21 04:54:51

BufferedReader.ready() 方法的行为符合指定:

Reader.ready() javadoc 表示以下内容:

[返回] 如果保证下一个 read() 不会阻塞输入,则返回 true,否则返回 false。请注意,返回 false 并不能保证下一次读取会被阻塞。

然后是 BufferedReader。 read() javadoc 的内容如下:

告知该流是否已准备好读取。如果缓冲区不为空,或者底层字符流已就绪,则缓冲字符流已就绪。

如果将这两者放在一起,很明显,在字符可用的情况下,BufferedReader.ready()可以返回false。简而言之,您不应该依赖 ready() 来测试逻辑文件结束或流结束。

The BufferedReader.ready() method is behaving as specified:

The Reader.ready() javadoc says the following:

[Returns] true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

Then the BufferedReader.ready() javadoc says the following:

Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.

If you put these two together, it is clear that BufferedReader.ready() can return false in situations where are characters available. In short, you shouldn't rely on ready() to test for logical end-of-file or end-of-stream.

执妄 2024-10-21 04:54:51

这是我们多年来一直使用的方法 - 不确定这是否是“标准”方法。我想听听有关直接使用 URL.openURLStream() 的优缺点的评论,以及这是否会导致 OP 的问题。此代码适用于 HTTP 和 HTTPS 连接。

 URL  getURL = new URL (servletURL.toString() + identifier+"?"+key+"="+value);      
 URLConnection uConn = getURL.openConnection();

 BufferedReader br = new BufferedReader (new
                            InputStreamReader (uConn.getInputStream()));
 for (String s = br.readLine() ; s != null ; s = br.readLine()) {
      System.out.println ("[ServletOut] " + s);
      // do stuff with s
 }               
 br.close();

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.

 URL  getURL = new URL (servletURL.toString() + identifier+"?"+key+"="+value);      
 URLConnection uConn = getURL.openConnection();

 BufferedReader br = new BufferedReader (new
                            InputStreamReader (uConn.getInputStream()));
 for (String s = br.readLine() ; s != null ; s = br.readLine()) {
      System.out.println ("[ServletOut] " + s);
      // do stuff with s
 }               
 br.close();
七秒鱼° 2024-10-21 04:54:51

基本上,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....

濫情▎り 2024-10-21 04:54:51

如果您想使用 in.ready(),以下内容对我来说效果很好:

    for (int i = 0; i < 10; i++) {
        System.out.println("is InputStreamReader ready: " + in.ready());
        if (!in.ready()) {
            Thread.sleep(1000);
        } else {
            break;
        }
    }

If you want to use in.ready(), the following worked for me well:

    for (int i = 0; i < 10; i++) {
        System.out.println("is InputStreamReader ready: " + in.ready());
        if (!in.ready()) {
            Thread.sleep(1000);
        } else {
            break;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文