Link to home
Start Free TrialLog in
Avatar of summer_soccer
summer_soccer

asked on

restore from saved objects?

Hi nice folks,

   I'm writing some code relevant to object I/O. The program saves
three objects, a Long, a Vector of some self defined class, and a
Hashset of some self defined class, to a file. Later on it reads
out those objects from the saved file. Problem occurs right here.
It throws out a file.java.io.EOFException when reading the very
first objects. Does anybody have any clue what's wrong here? BTW,
the java compiler is a 1.3 version one. Thanks.

SR
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

You'll have to post your source code :-)

Tim
Avatar of FaithNoMore
FaithNoMore

If you posted your current code, it might give us a better idea.
Sorry for extra post, Tim just beat me :)
Hee hee :-)
Avatar of summer_soccer

ASKER

Actually it's part of a big package. Actually, now I've tried  as follows:

try{
String storedStatesFile = "hoststates-data";
ObjectOutputStream outToStatesFile = new ObjectOutputStream(new FileOutputStream(storedStatesFile));
         outToStatesFile.writeLong(System.currentTimeMillis());
outToStatesFile.close();
       
ObjectInputStream inFromStatesFile = new ObjectInputStream(new FileInputStream(storedStatesFile));

Long lastSaveTime = inFromStatesFile.readLong();
}
catch(Exception e) {
  System.out.println("Exception: "+e);
}

The program threw out an EOFException when it tries to readLong from storedStatesFile.

Does anybody have any clue on this? Thanks a lot.

SR
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and don't forget:

  inFromStatesFile.close() ;
Could try this:

outToStatesFile.writeObject(new Long(System.currentTimeMillis()));

Long lastSaveTime = (Long)inFromStatesFile.readObject();

also, inFromStatesFile.readLong(); returns a 'long' value, not Long.
Yeah...like I said ;-)
if that doesnt help (which i am pretty sure IT WILL) see if the object is ever written to the file
No offence, folks, but I'd prefer saving 3 different kinds of objects into 3 different files, or else make another class which would wrap these 3 objects in it, and then write that one to the file.

Cheers,

Mayank.
easy doing that also. save all your objects into a vector and save vector in the file. next time load the vector and cast the elements back to your own objects
Tim your too fast for me ;).