Java question

Chat and discussion not related to either Marathon or Aleph One. Please keep things at least mildly interesting, though.
Post Reply
User avatar
gmanyo
Mjolnir Mark IV
Posts: 767
Joined: Feb 9th '09, 13:28
Contact:

Does anyone actually use EOFExceptions to designate the end of a file when using data and object streams, or is there a better way to do it? I've read that it works alright, but it just seems a bit sketchy to me: I would think that one would just put an end-of-file marker on the end.
Image
User avatar
treellama
Vidmaster
Posts: 6110
Joined: Jun 2nd '06, 02:05
Location: Pittsburgh
Contact:

Ordinarily this would be a GOOGLE IT question but you're so off base you don't even understand what an EOFException is. It's not something you PUT on the end of a file, it gets thrown when you try to read past the end of the file. There's no such thing as an end-of-file marker, it just ends.

Whether you use exceptions for this is a matter of taste in Java, personally I wouldn't, since any of the read functions will tell you if you hit the end of the file.
Last edited by treellama on Jan 13th '10, 16:34, edited 1 time in total.
User avatar
gmanyo
Mjolnir Mark IV
Posts: 767
Joined: Feb 9th '09, 13:28
Contact:

Treellama wrote:Ordinarily this would be a GOOGLE IT question but you're so off base you don't even understand what an EOFException is. It's not something you PUT on the end of a file, it gets thrown when you try to read past the end of the file. There's no such thing as an end-of-file marker, it just ends.

Whether you use exceptions for this is a matter of taste in Java, personally I wouldn't, since any of the read functions will tell you if you hit the end of the file.
Ah, thanks. I figured out my main problem was that I was creating a new file every time I tried to instantiate the ObjectInputStream object, thus instantly stepping over the end of the 0-size file, and would therefore an exception was thrown. I don't like using exceptions in places where there isn't really a problem, so I'll try it your way. But in what way do the read functions "tell" you that you are at the end of a file?
Image
User avatar
treellama
Vidmaster
Posts: 6110
Joined: Jun 2nd '06, 02:05
Location: Pittsburgh
Contact:

Read the documentation! read() returns -1 when it hits the end of the stream.

If you're using the primitive read functions on ObjectInputStream you should already know how much data is available, and read only that much from the stream. I would still avoid using EOFException to tell you when the end of the stream is, but you sure better handle it just in case someone gives you invalid data.
User avatar
gmanyo
Mjolnir Mark IV
Posts: 767
Joined: Feb 9th '09, 13:28
Contact:

Thanks for the help! I finished it: if you want to play it, here it is. It's a super simple and super stupid game where you try to get Robots to crash into each other; just a copy of an old game, really. Won't work if you don't have the Java Virtual Machine, though, but I think most computers do.
Attachments
Robotz.zip
(15.58 KiB) Downloaded 214 times
Image
Post Reply