Newbie: How do I read/write my objects to disk?

G

!George Sgouros

Hello everybody!

I am trying to store the instances of my objects to disk.

For the moment I have only managed to write the code which saves an object
to a file, although I am not sure it is correct. Here's the code (executed
just before program termination):

System.out.print(" Saving data to disk...");
File MyDataFile = new File("datafile.txt");
FileOutputStream MyFile = new FileOutputStream(MyDataFile);
ObjectOutputStream MyOutputStream = new ObjectOutputStream(MyFile);
MyOutputStream.writeObject(vehList);
MyFile.close();
System.out.println("done");

where vehList is an instance of class VehicleList which extends the Vector
Java Class

So here are my questions:

1. Is there an easier way to do the above?
2. How can I reconstruct the vehList object when the program initiates?

Many thanks in advance

George
 
G

Gordon Beaton

I am trying to store the instances of my objects to disk.
[...]

So here are my questions:

1. Is there an easier way to do the above?

That's pretty much the way to do it.

You should close the ObjectOutputStream *before* closing MyFile though.
2. How can I reconstruct the vehList object when the program
initiates?

By doing the exact opposite steps, i.e. open an ObjectInputStream and
read the Object.

/gordon
 
M

Marco Schmidt

!George Sgouros:

[...]
1. Is there an easier way to do the above?

As for using ObjectOutputStream - no, I think that's the most simple
way. You don't need a File object, but can't get around opening,
writing and closing.

There are other ways to save objects. Object-oriented databases,
persistence frameworks, mappings from objects to relational databases,
and so on.

BTW, I'd suggest to use standard Java naming conventions for your
variable names. Let everything start with a lower case letter, so
myFile instead of MyFile. Typical stream names are in and out. These
are small things, but they improve readability for most other people.

Besides, I wouldn't name the object file ".txt" - it's not a text
file.
2. How can I reconstruct the vehList object when the program initiates?

ObjectInputStream in = new ObjectInputStream(new
FileInputStream("datafile.txt"));
Object list = in.readObject();
in.close();

With a typecast you can convert the object to its original
representation:

Vector vec = (Vector)list;

This will fail if you didn't write a Vector, so replace that type with
whatever you used.

Regards,
Marco
 
A

Andrew Thompson

!George Sgouros said:
Hello everybody!

I am trying to store the instances of my objects to disk.

For the moment I have only managed to write the code which saves an object
to a file, although I am not sure it is correct. Here's the code (executed
just before program termination):

System.out.print(" Saving data to disk...");
File MyDataFile = new File("datafile.txt");

Technically a serialized java object is not
'text', so perhaps it would be better to
refer to the file as something like,
'VehList.ser'.
FileOutputStream MyFile = new FileOutputStream(MyDataFile);
ObjectOutputStream MyOutputStream = new ObjectOutputStream(MyFile);
MyOutputStream.writeObject(vehList);
MyFile.close();
System.out.println("done");

where vehList is an instance of class VehicleList which extends the Vector
Java Class

So here are my questions:

1. Is there an easier way to do the above?

...just how much easier do you want it?
2. How can I reconstruct the vehList object when the program initiates?

Almost exactly the opposite of what you have..
Change every occurrence of 'Output' to 'Input',
but change this line..
MyOutputStream.writeObject(vehList); ...to

VehList vehList = (VehList)MyInputStream.readObject();

HTH
 
N

nos

Andrew Thompson said:
Technically a serialized java object is not
'text', so perhaps it would be better to
refer to the file as something like,
'VehList.ser'.


..just how much easier do you want it?


Almost exactly the opposite of what you have..
Change every occurrence of 'Output' to 'Input',
but change this line..

VehList vehList = (VehList)MyInputStream.readObject();

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site

you can make an XML file like this -
example objects can be anything

ee = new XMLEncoder(new BufferedOutputStream(new
FileOutputStream(filename)));
}
ee.writeObject(title);
ee.writeObject(date);
ee.writeObject(format);
ee.writeObject(new Integer(size));
ee.writeObject(filename);
ee.writeObject(xarray);
ee.writeObject(yarray);
ee.writeObject(earray);
ee.close();
 
S

Sudsy

nos wrote:
you can make an XML file like this -
example objects can be anything

ee = new XMLEncoder(new BufferedOutputStream(new
FileOutputStream(filename)));

Um, not really. There have been a number of discussions recently
regarding the limitations of this class. Refer to the javadocs
and you'll find that it's only meant to be used with beans, with
all the conventions and restrictions which apply.
 
N

nos

Sudsy said:
nos wrote:


Um, not really. There have been a number of discussions recently
regarding the limitations of this class. Refer to the javadocs
and you'll find that it's only meant to be used with beans, with
all the conventions and restrictions which apply.

Yes I heard that it is intended for beans only, but I also heard
that almost any class can be a bean (simple rules to qualify)
and that it works for ordinary objects, so I tried it and it works
for me. The only thing is that it does not store the name of the
object in the file so when you read the file back in you have
to have the same order of the objects as when you write it out.
 
T

Tor Iver Wilhelmsen

Andrew Thompson said:
Technically a serialized java object is not
'text'

unless you use the XML serializer; but then it should perhaps be .xml
not .txt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top