input.read() func. stack the while loop [closed]
After connecting to server, I run some commands on server and then trying to take the server knowledge to console with;
int i = 0;
byte[] 开发者_Go百科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();
}
but if I run this loop more than one and if the line (which it try to read) is empty it stacks and don't let the program to stop.I tried to check empty line by string check or -1 check but it doesn't work. How can I solve this problem?
Change that:
while ((i = input.read()) != 10 )
To that:
while (((i = input.read()) != 10 ) && (i != -1) )
To check for EOF.
精彩评论