java 从 BufferedReader (readLine) 读取返回 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10202515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:02:09  来源:igfitidea点击:

Reading from a BufferedReader (readLine) returns null?

javastringbufferedreader

提问by Charlie

I am currently trying to read a Stringfrom a BufferedReaderbut cant find a way to do this...

我目前正在尝试String从 a 中读取 aBufferedReader但无法找到一种方法来执行此操作...

Of course I tried

我当然试过了

BufferedReader inStream = null;
inStream = new BufferedReader(new InputStreamReader(client.getInputStream()));
String test = inStream.readLine();

However the result turns out as null when trying to print to a screen even though the BufferedReader inStreamis equal to some kind of message.

然而,即使BufferedReader inStream等于某种消息,尝试打印到屏幕时结果也为空。

回答by Attila

Based on the documentation, the BufferedReader.readLine()returns nullonly when the end of the stream is reached. This means if the first call to readLine()returns null, there was nothing in the input stream to begin with.

根据文档,只有在到达流的末尾时才BufferedReader.readLine()返回null。这意味着如果第一次调用readLine()返回null,则输入流中没有任何内容可以开始。