开发者

input.read() func. stack the while loop [closed]

开发者 https://www.devze.com 2023-03-29 03:15 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号