wood burning stoves*
The moose likes Beginning Java and the fly likes BufferedReader problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BufferedReader problem" Watch "BufferedReader problem" New topic
Author

BufferedReader problem

Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hey JavaGurus!

Ive got the following code:



The code runs well until the last line from the BufferedReader object (din) is printed out in the while loop. The flow seems to freeze after that line has been printed. It doesnt leave the loop, yet the while-loop doesnt seem to keep on looping.

Any thoughts?
Thanks in advance


Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you reading from a file or from a Socket? If it's a Socket, it might be that readLine() doesn't return null after the data is read from the Socket.
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
You may want to also try
while ( din.ready() ) {
System.out.println(din.readLine());
}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

You seem indeed to be reading from a socket. A socket doesn't have an "end of file", i.e. there is never a "last line" to be read, so readLine() will never return null. When there's no data coming in on the socket connection, readLine() will just block and wait until data comes in.

Calling ready(), like Jeff suggests, is not a reliable way to check if you've read everything. The computer on the other side of the socket may send data at any speed it likes - it may pause for a short while in between sending data, and for that short while your call to ready() will return false, and you'll think you're done reading the data, while that isn't really so.

The only way you can get this right is that you have to build something into the application-level protocol that you're talking over the socket. In that protocol, there must be some sign that tells you that you have all the data. In other words, the other side has to send you a command or special string that indicates that you're at the end of the data, and in your receiving end you look for that to see if you're done.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hmm, I read from URL until null. Isn't that just a dressed up socket? I think you'll get null if the other end closes the socket. Here's a bit from the socket tutorial. It uses both null and a terminator in the protocol.

[ November 22, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedReader problem
 
Similar Threads
need help building a =hiearchical= JSON object
reading csv file
Words with capital letters
bufferedwriter/filewriter...
Reading/Writing Formatted Text