2011-08-17 44 views
1

连接到服务器后,我在服务器上运行一些命令,然后尝试将服务器知识用于控制台;input.read()func。堆栈while循环

int i = 0; 
byte[] bx = new byte[1]; 
try { 
    while ((i = input.read()) != 10) { 
     bx[0] = (byte) i; 
     System.out.print(new String(bx)); 
    } 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} 

,但如果我运行这个循环不止一个,如果(它尝试读取)行是空的,则它堆积,不要让程序stop.I试图通过检查字符串检查空行或-1检查,但它不起作用。我怎么解决这个问题?

+3

“it stacks”是什么意思? –

+1

uppsss对不起,我的意思是它锁定,(暂停):) –

回答

1

变化是:

while ((i = input.read()) != 10) 

要的是:

while (((i = input.read()) != 10) && (i != -1)) 

check for EOF

+1

我试过了,它不工作,因为它在input.read()中堆栈并且无法继续... –