This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes End Of File Marker Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "End Of File Marker" Watch "End Of File Marker" New topic
Author

End Of File Marker

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
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

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Huh?

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

Drew
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296


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

Joined: May 13, 2001
Posts: 296
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

Joined: Jan 29, 2003
Posts: 8791
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.


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
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: End Of File Marker
 
Similar Threads
marker interface
Help Needed : File comparision irrespective of the order
NX: about Property file, Policy file and Logger file
How to ignore or remove comments from a java file with the help of another java file.
questions on marker interfaces