File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes java.io.EOFException using readObject Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java.io.EOFException using readObject" Watch "java.io.EOFException using readObject" New topic
Author

java.io.EOFException using readObject

Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
hi there
i am trying to write a class that save a temporary binary file that holds records for a game i'm working on. i keep getting java.io.EOFException and i get it in this line
"ois = new ObjectInputStream(fis);", so i didn't even get to read from the file. can any one tell me what am i doing wrong? this is my first time working with files I/O
so here's the code:


and the error i keep getting

thank you anyway!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

The first time you run this, there will be no high scores file to read, since you've never written one; therefore I'd expect a FileNotFoundException on the line before the one where you're getting the EOFException. But perhaps you've created an empty "records.dat" file somehow, and so instead you're getting the EOFException.

There are some other major issues here. The method that writes the high scores file will not do so properly. Note that the first thing it does is write the number of scores to the ObjectOutputStream; this is before the ObjectOutputStream is created, so you'll get a NullPointerException if that method were ever to be called.

Part of the problem is that you've made some design choices here that aren't conducive to clear, understandable code. In particular, keeping the various streams as member variables makes no sense; a stream should virtually always be opened and closed in the same method, which means that method is the perfect place for a local variable to hold them.


[Jess in Action][AskingGoodQuestions]
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
okay i've made some cahnges. situation now is a bit better - i get no errors but when i try to read from the file i get nothing.
i did a few things:
i've changed the file and objects members to be local variables.
also no the list is written to the file as one ArrayList objects, instead of writing each record by itself (btw should i clear the file each time i save the list to the file??)
anyway as i said i have no errors, but when i tried to read from the file i get 'Records:' which of course comes from my HighScores toString method
here's the relevant code:
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
In this code:
Why did you comment out the close() block? You should always issue a close(). In fact, you should issue the close() in a finally {} block.
Using your current code, look at the size of your file after you write the object (using your favorite tool such as Windows Explorer or command line shell). I suspect its length may be very small (0?)
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
Tom Reilly wrote:In this code:
Why did you comment out the close() block? You should always issue a close(). In fact, you should issue the close() in a finally {} block.
Using your current code, look at the size of your file after you write the object (using your favorite tool such as Windows Explorer or command line shell). I suspect its length may be very small (0?)


okay i've deleted the comment marks, still doesn't work. the file is 58KB. is that what you meant i should check?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
This may have been a readObject() problem when you started but is no longer. I compiled and ran your code in a debugger. I changed the main() code to be (added some debug messages and un-commented out some code):
And I changed another line of code in the following method. There is a bug. Can you see it?
The first time I ran it, I got the following output:
The second time I ran it, I got this:
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
thank you very much
honestly i don't see the problem with my addHighScore method, and i can't see the difference between what you've modified and mine.
maybe you have mistakenly wrote mine instead of yours?

anyway, problem remains. i still don't get the output you got.

p.s.
how come the code creates a new file, when i never used a method to do it? thanks
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Actually, I intentionally showed your code rather than mine. This site is about learning rather than providing code. I'll give you another hint. Why would you only use an object reference when it is null?
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
okay you're right, i found it.
so i've changed

to and it works...
so i think that's okay.
one last question: when i save the scores to the .dat file i want to delete the object
that's already written and write the new updated one. what method can i use to do that?
thanks...

p.s. again
did you notice my question from before about the fact that a new file is created if there is none?
i was wondering why it happens as i never wrote anything to take care of it

thanks!
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Actually, my solution was to check Why would you only want to add a high score if the _currentList is empty?
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
Tom Reilly wrote:Actually, my solution was to check Why would you only want to add a high score if the _currentList is empty?

oh because the records should be sorted.
if the list is empty (.size()==0) then i add the record at the first place, otherwise i go through the ArrayList
and add it in the right place
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.EOFException using readObject