• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the javadoc of the readline method.
 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic