bufferedReader.readLine() 永远不会等于 null
bufferedReader.readLine() will never equal null
我正在编写一些代码,用于从文本文件中提取名称和 ID。为此,我使用了一个 while 循环。看起来 while 循环条件总是为真,程序永远不会跳出 while 循环。代码如下所示任何帮助将不胜感激。谢谢
while((line = br.readLine()) != null) {
line = br.readLine();
endOfFirstName = line.indexOf(",");
first_name = line.substring(0, endOfFirstName);
endOfLastName = line.indexOf(" ");
last_name = line.substring(endOfFirstName + 1, endOfLastName);
id = line.substring(endOfLastName + 1);
}
这个条件
while((line = br.readLine()) != null)
用于处理结束/关闭的流-
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.
您必须处理自己的循环终止 - 取决于您认为值得关闭循环的内容。
即在标准 HTTP 请求中,这是通过空白换行符完成的。
编辑
您还丢弃了流中的所有其他行
去掉多余的
line = br.readLine();
因为这已经由循环处理了。
我正在编写一些代码,用于从文本文件中提取名称和 ID。为此,我使用了一个 while 循环。看起来 while 循环条件总是为真,程序永远不会跳出 while 循环。代码如下所示任何帮助将不胜感激。谢谢
while((line = br.readLine()) != null) {
line = br.readLine();
endOfFirstName = line.indexOf(",");
first_name = line.substring(0, endOfFirstName);
endOfLastName = line.indexOf(" ");
last_name = line.substring(endOfFirstName + 1, endOfLastName);
id = line.substring(endOfLastName + 1);
}
这个条件
while((line = br.readLine()) != null)
用于处理结束/关闭的流-
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.
您必须处理自己的循环终止 - 取决于您认为值得关闭循环的内容。
即在标准 HTTP 请求中,这是通过空白换行符完成的。
编辑
您还丢弃了流中的所有其他行
去掉多余的
line = br.readLine();
因为这已经由循环处理了。