?
Solved

restore from saved objects?

Posted on 2003-03-27
13
Medium Priority
?
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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