?
Solved

restore from saved objects?

Posted on 2003-03-27
13
Medium Priority
?
177 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:summer_soccer
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8221215
You'll have to post your source code :-)

Tim
0
 
LVL 1

Expert Comment

by:FaithNoMore
ID: 8221225
If you posted your current code, it might give us a better idea.
0
 
LVL 1

Expert Comment

by:FaithNoMore
ID: 8221232
Sorry for extra post, Tim just beat me :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 8221294
Hee hee :-)
0
 

Author Comment

by:summer_soccer
ID: 8221299
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
0
 
LVL 35

Accepted Solution

by:
TimYates earned 80 total points
ID: 8221355
try
{
  String storedStatesFile = "hoststates-data";
  ObjectOutputStream outToStatesFile = new ObjectOutputStream(new FileOutputStream(storedStatesFile));
  outToStatesFile.writeObject( new Long( System.currentTimeMillis() ) ;
  outToStatesFile.close();
       
  ObjectInputStream inFromStatesFile = new ObjectInputStream(new FileInputStream(storedStatesFile));
  Long lastSaveTime = (Long)inFromStatesFile.readObject();
}
catch(Exception e)
{
 System.out.println("Exception: "+e);
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8221356
and don't forget:

  inFromStatesFile.close() ;
0
 
LVL 1

Expert Comment

by:FaithNoMore
ID: 8221367
Could try this:

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

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

also, inFromStatesFile.readLong(); returns a 'long' value, not Long.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8221568
Yeah...like I said ;-)
0
 
LVL 2

Expert Comment

by:glottis
ID: 8222654
if that doesnt help (which i am pretty sure IT WILL) see if the object is ever written to the file
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8223629
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.
0
 
LVL 2

Expert Comment

by:glottis
ID: 8223792
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
0
 
LVL 1

Expert Comment

by:FaithNoMore
ID: 8224644
Tim your too fast for me ;).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question