• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

End Of File Marker

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I write a DataOutputStream, do I need to include the marker at the end of the file (ie. -1)?

For some reason I thought that Java did this for me, but I was reading back some of the variable length files I had written and keep getting an EndOfFile Exception (I was checking for -1). The data is a series of shorts.

Thanks,

Drew
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no end-of-file marker.

If you think about it, you see, that one value would be needed, reserved only for the last (character? byte? short?) in a file.
What a waste!

If you read bytes (0-255) and get a -1, it is out of the byte-scope.
It wasn't sended.

Some applications defined their own 'eof'-character.
I don't know where they went.

You may add files together:

You would get two eofs in the middle of the file.

Or the command would have to remove it.
The filesize would differ at two - or the dir-command would have to substract one for each file.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh?

Sorry, I don't understand what you're saying.

Drew
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan is describing why having a physical byte that marks the end of a file is a bad idea. Simple enough to say there is no such thing. You do not have to add an EOF marker to your output. If you are getting an EOF exception the problem is in your program logic. Feel free to post your code and we can look it over.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The size of the data is unknown.

If I add a -1 to the end of the data, this code works.

If not, it still works, but throws the EndOfFile Exception.

Drew
[ October 12, 2004: Message edited by: Drew Lane ]
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not sure what the best way to handle this is.

It seems to me that if you don't know the length of your data then you have three choices:

1. Store the content length somewhere and read it back
2. Use a marker at the end of the file
3. Catch the end of file exception

Is there a better way that I'm not thinking of?

Thanks,

Drew
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's somethign I use to read a file and write it to another stream. I read largish chunks of data at a time rather than looking for the -1 end of file return.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the Java tutorial today and it seemed to indicate that catching the EndOfFile Exception is perfectly fine for a DataInputStream.

Maybe I shouldn't worry about it...

Drew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic