aspose file tools*
The moose likes Beginning Java and the fly likes How to detect EOF when using java.io.BufferedReader? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to detect EOF when using java.io.BufferedReader?" Watch "How to detect EOF when using java.io.BufferedReader?" New topic
Author

How to detect EOF when using java.io.BufferedReader?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
I'm reading stdin using BufferedReader and I noticed there is no function to detect the end of file! How might I enhance the program below to only echo "data goes here"? I'm running java 1.6 on cygwin/windows XP Server.

It seems odd that I could even read past the EOF. I cannot do this in gnu C++ with a similar bash procedure.

Thanks,
Siegfried

Here is the output:

Here is the source code:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Take a look at the javadoc of the readline method.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
I read the javadoc at http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html before posting.

It says that readLine() will return null at the end of stream. Clearly this is not happening: see my output listing. It is going past the end-of-file. The fact that gnu C++ does not do this makes me think that this is a bug with readLine. Do you think it could be a bug with bash?

I tried calling the read function but could not figure out how to convert a char[] to a String. I tried using the String constructor and toString but neither worked.

The program below works with the embedded bash script. I do not get XML syntax errors because it reads beyond the EOF. Something must be able to detect EOF properly from a bash script. Can anyone tell me how to make readLine behave? Or make read behave?
Thanks,
Siegfried

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Your output shows that null is being returned. It is behaving exactly as it is described.

readLine returns null at the end of the file. It does not throw an exception.

You have to handle it when it returns null, ie stop calling readLine.
[ December 08, 2007: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
But why is it echoing the "EOF" and "rm ...."? That stuff is after the end-of-file! That is wrong! Does my java code have to look for the string "EOF"? bash is supposed to be doing that and is doing that with gnu g++ and the second java program ApplyXPath I posted. Why is it that the second java program ApplyXPathcan read the XML correctly and stop before the string "EOF" but not the first?

I guess it is a bug in readLine. I wish I had the source to readLine handy so I could find a better workaround.

Thanks,
Siegfried
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It looks like the end of your here document is not being recognized, and the letters EOF are being transmitted as "EOF" rather than interpreted as an end-of-file. This is something that should happen in bash before your java program ever sees it. The problem could be something as simple as having whitespace in front of the "EOF" - this delimiter must be the first thing on a line. Anyway it's not a Java issue; it's something in your bash script.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And the source to readLine() should be readily available within $JAVA_HOME/src.zip, part of your JDK installation. But I really don't think there's any problem in readLine() - it's in the bash script.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I agree with Jim, it's definitely a problem with the script. The line

java package_name_goes_here.k <<EOF

is obviously not doing what you think it does.

Unfortunately, I don't know enough about shell script programming to be of more help.
 
jQuery in Action, 2nd edition
 
subject: How to detect EOF when using java.io.BufferedReader?
 
Similar Threads
Wanted: Help serializing Generic Maps!
How to have multiple public classes in one file for spring?
How to Serialize Maps?
java6 console
Sample on how to work with XPATH