How should I read from a buffered reader?

20,585

Solution 1

When the socket on the other end is closed, the reader should return a null string. This is the condition that you are looking for. To handle the exception, wrap the reading loop in a try/catch block.

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

You might find this tutorial on reading/writing from/to a socket in Java, helpful.

Solution 2

For your first question:

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.

BufferedReader.readLine() does not return true upon success. It returns a String containing the line that was read. If the end of the stream is reached, it returns null.

Your second 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?

You should wrap your code in a try/catch block. If you don't want to handle the caught exception, simply leave that part blank (not recommended)

try {
    while ((inputLine = input.readLine()) != null) {
        System.out.println("I got a message from a client: " + inputLine);
    }
} catch (Exception e) {
    //handle exception
}

Solution 3

The reader's readLine() will return a string value when it has something read, an empty string when there isn't anything yet, and null when the connection is closed.

I would recommend wrapping a try/catch around your block of code with the IO function and handle errors appropriately.

Share:
20,585
Roman
Author by

Roman

Updated on July 14, 2022

Comments

  • Roman
    Roman almost 2 years

    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?