How should I read from a buffered reader?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-23T13:15:29Z Indexed on 2010/03/23 13:23 UTC
Read the original article Hit count: 338

I have the following example of reading from a buffered reader:

while ((inputLine = input.readLine()) != null) {
   System.out.println("I got a message from a client: " + inputLine);
}

The code in the loop println will be executed whenever something appears in the buffered reader (input in this case). In my case, if a client-application writes something to the socket, the code in the loop (in the server-application) will be executed.

But I do not understand how it works. inputLine = input.readLine() waits until something appears in the buffered reader and when something appears there it returns true and the code in the loop is executed. But when null can be returned.

There is another question. The above code was taken from a method which throws Exception and I use this code in the run method of the Thread. And when I try to put throws Exception before the run the compiler complains: overridden method does not throw exception. Without the throws exception I have another complain from the compiler: unreported exception. So, what can I do?

© Stack Overflow or respective owner

Related posts about java

Related posts about bufferedreader