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.
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
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
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
Joined: Aug 11, 2000
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.
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
Joined: Jan 30, 2000
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.
Joined: Jul 11, 2001
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.
subject: How to detect EOF when using java.io.BufferedReader?