从 InputStream 读取 - 陷入循环
Reading from InputStream - stuck in cycle
我有这段代码:
(这段代码在另一个循环中,即循环3次)
...
text = "";
while((c = is.read())!=-1){
if(prev == '\r' && c == '\n'){
break;
}
text = text + (char) c;
prev = (char) c;
}
System.out.println(text);
...
is
是InputStream,c
是int,prev
是char
使用这段代码,我从 InputStream 构建了一个字符串。每次我得到 \r\n 时,读数都应该停止。然后它又开始了。除了一件事,一切都很好。我得到的流如下所示:
1233\r\n544\r\nX
此输入流后没有分隔符
有了这个,我从第一个循环中得到字符串 1233
,从第二个循环中得到字符串 544
。但我不会得到最后一个 X
,因为循环不会就此停止 - 我不知道为什么。我认为使用 is.read()=!-1
循环应该在流结束时停止。但事实并非如此。该程序卡在该循环内。
你的问题不清楚,但这里是:
while( ( c = is.read() ) != -1 )
{
if(prev == '\r' && c == '\n')
{
break;
}
text = text + (char) c;
prev = (char) c;
}
注意执行顺序。检查 \r\n
并退出循环,然后将当前字符附加到 text
.
你觉得这个逻辑有什么问题吗?
你也说
the cycle should stop when the stream ends. But it doesn't. The
program is stuck inside that cycle.
如果最后两个字节永远不会 \r\n
或者如果 流永远不会关闭 它永远不会 end 而它将以任何方式删除最后一个 \n
!
那么循环永不结束或 \n
永远不会被追加是什么?
如果您希望循环在流 和 的末尾结束,当检测到 \r\n
时,您需要重新排序您的逻辑。
垃圾进垃圾出:
假设你的 InputStream
. 中实际上有 \r\n
对,你确定它们在那里吗?,步骤调试会告诉你肯定的!
public static void main(final String[] args)
{
final InputStream is = System.in;
final StringBuilder sb = new StringBuilder(1024);
try
{
int i;
while ((i = is.read()) >= 0)
{
sb.append((char)i);
if (sb.substring(sb.length()-2).equals("\r\n"))
{
break;
}
}
}
catch (final IOException e)
{
throw new RuntimeException(e);
}
System.out.print(sb.toString());
}
You need to stop and learn how to use the step debugger that is in
your IDE. This would not be a question if you just stepped through
your code and put a few break points where things were not as you
wanted or expected them.
我有这段代码:
(这段代码在另一个循环中,即循环3次)
...
text = "";
while((c = is.read())!=-1){
if(prev == '\r' && c == '\n'){
break;
}
text = text + (char) c;
prev = (char) c;
}
System.out.println(text);
...
is
是InputStream,c
是int,prev
是char
使用这段代码,我从 InputStream 构建了一个字符串。每次我得到 \r\n 时,读数都应该停止。然后它又开始了。除了一件事,一切都很好。我得到的流如下所示:
1233\r\n544\r\nX
此输入流后没有分隔符
有了这个,我从第一个循环中得到字符串 1233
,从第二个循环中得到字符串 544
。但我不会得到最后一个 X
,因为循环不会就此停止 - 我不知道为什么。我认为使用 is.read()=!-1
循环应该在流结束时停止。但事实并非如此。该程序卡在该循环内。
你的问题不清楚,但这里是:
while( ( c = is.read() ) != -1 )
{
if(prev == '\r' && c == '\n')
{
break;
}
text = text + (char) c;
prev = (char) c;
}
注意执行顺序。检查 \r\n
并退出循环,然后将当前字符附加到 text
.
你觉得这个逻辑有什么问题吗?
你也说
the cycle should stop when the stream ends. But it doesn't. The program is stuck inside that cycle.
如果最后两个字节永远不会 \r\n
或者如果 流永远不会关闭 它永远不会 end 而它将以任何方式删除最后一个 \n
!
那么循环永不结束或 \n
永远不会被追加是什么?
如果您希望循环在流 和 的末尾结束,当检测到 \r\n
时,您需要重新排序您的逻辑。
垃圾进垃圾出:
假设你的 InputStream
. 中实际上有 \r\n
对,你确定它们在那里吗?,步骤调试会告诉你肯定的!
public static void main(final String[] args)
{
final InputStream is = System.in;
final StringBuilder sb = new StringBuilder(1024);
try
{
int i;
while ((i = is.read()) >= 0)
{
sb.append((char)i);
if (sb.substring(sb.length()-2).equals("\r\n"))
{
break;
}
}
}
catch (final IOException e)
{
throw new RuntimeException(e);
}
System.out.print(sb.toString());
}
You need to stop and learn how to use the step debugger that is in your IDE. This would not be a question if you just stepped through your code and put a few break points where things were not as you wanted or expected them.