gpt4 book ai didi

java - 为什么 BufferedReader 无法读取 InputStream 的最后一行?

转载 作者:行者123 更新时间:2023-11-29 10:15:45 25 4
gpt4 key购买 nike

*注意:澄清一下,也许这个问题关于 InputStream 是“活着的”不是很清楚。与 telnet 天气服务的连接(请参阅下面的链接)保持打开状态。目标是获取来自服务器的所有行。*

从示例 Apache WeatherTelnet 构建 code ,我正在使用 InputStream.read使用 chars 方法一次输出一个字符(受 javamex tutorial 习语启发):

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/Teln/dist/Teln.jar
------------------------------------------------------------------------------
* Welcome to THE WEATHER UNDERGROUND telnet service! *
------------------------------------------------------------------------------
* *
* National Weather Service information provided by Alden Electronics, Inc. *
* and updated each minute as reports come in over our data feed. *
* *
* **Note: If you cannot get past this opening screen, you must use a *
* different version of the "telnet" program--some of the ones for IBM *
* compatible PC's have a bug that prevents proper connection. *
* *
* comments: jmasters@wunderground.com *
------------------------------------------------------------------------------

Press Return to continue:
^Cthufir@dur:~$

这是期望的输出。但是,使用 BufferedReader 读取 InputStream 会导致删除最后一行。 (或者,至少,它没有打印到控制台。)错误的输出,使用 lines 方法:

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/Teln/dist/Teln.jar

------------------------------------------------------------------------------

* Welcome to THE WEATHER UNDERGROUND telnet service! *

------------------------------------------------------------------------------

* *

* National Weather Service information provided by Alden Electronics, Inc. *

* and updated each minute as reports come in over our data feed. *

* *

* **Note: If you cannot get past this opening screen, you must use a *

* different version of the "telnet" program--some of the ones for IBM *

* compatible PC's have a bug that prevents proper connection. *

* *

* comments: jmasters@wunderground.com *

------------------------------------------------------------------------------



^Cthufir@dur:~$
thufir@dur:~$

StreamReader代码:

package teln;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StreamReader {

private StreamReader() {
}

StreamReader(InputStream inputStream) throws IOException {
lines(inputStream);
}

private void chars(InputStream inputStream) throws IOException {
do {
char ch = (char) inputStream.read();
System.out.print(ch);
} while (true);
}

private void lines(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = "";
do {
System.out.println(line);
} while ((line = br.readLine()) != null);

System.out.println(line);

}
}

大概最后一行不知何故是“空”?是否可以更改逻辑以便从 lines 打印最后一行,就像从 chars 打印一样?

最佳答案

您还没有解释数据的来源,但我怀疑它来自某个末尾没有行终止符并且没有关闭流的来源。

如果流已关闭 - 例如通过终止连接,如果这是一个客户端/服务器应用程序 - 那么您看到最后一行。 BufferedReader 肯定会返回最后一行数据,即使它没有以行结束符结束,但只有当它知道它到达了流的末尾时。

关于java - 为什么 BufferedReader 无法读取 InputStream 的最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18518217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com