File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes readLine() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "readLine()" Watch "readLine()" New topic


Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
Hi All,

My query is about readLine() and when it returns null.

I've got a chat client/server (from the good ole Head First guys) and in the server code it sets up the following runnable job that's loaded into a thread:

A message is sent to the server every time the "Send" button on the GUI (not shown) is clicked and ends up in the run() method above.

What is confounding me is that once the message has been sent, picked up in the run() method and printed out, the while loop loops back to the readLine() and as there isn't a message straight away won't it return null?

I know this isn't the case because if it were the run() method would complete and the thread would die. This doesn't happen because I can type in and send another message any time and it gets printed out so the thread must still be running.

So the question is why isn't readLine() returning null if there's nothing to read? and since it clearly isn't returnng null when will it return null

I know this must be simple but it's not clear to me and I've looked through the doco and it hasn't helped me so far.

So I'd be glad of some help.

Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
An input stream read method 'blocks until input data is available, the end of the stream is detected, or an exception is thrown.'
There is therefore a difference between no data being available and the end of the stream being reached. The latter will probably not happen until the socket is closed, but this is probably dependent on what the socket is connecting to.

Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
Ahh! Thanks Joanne.

I knew it had to be something like that and now you mention it I think I remember reading that somewhere

I agree. Here's the link:
subject: readLine()
It's not a secret anymore!