检查空值的Java字符串突然变为空

0 java runtime-error

我正在使用StreamGobbler来消耗进程的输出.

InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
int count = 0;
while ( (line = br.readLine()) != null);
{
    if(line == null){
         System.out.println("This shouldn't print out.");
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为字符串"这不应该打印出来"很明显.不应该打印出来,但确实如此.我不知道提供什么进一步的数据.

Ant*_*ony 6

在while表达式后面有一个分号,它将终止它,因此以下大括号被视为通用语句块.

基本上,分号会使它像这样对待.

while ( (line = br.readLine()) != null) {} //do nothing

if(line == null){
     System.out.println("This shouldn't print out.");
}
Run Code Online (Sandbox Code Playgroud)