writeObject with ArrayList of ArrayList?

K

Kaidi

Hi,
I am thinking of using this code to output and readin the data:

------------
File thefile = new File(outfile);
FileOutputStream fout = new FileOutputStream(thefile);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(vectorDB);
oos.close();
fout.close();
----------
the "vectorDB" is a ArrayList, with each element (item) of it another ArrayList.

Anyone can confirm that usign the above code, will it be properly saved to
file? (in another word, does writeObject can handle nested objects?)

Thanks a lot. :)
 
L

Lee Fesperman

Kaidi said:
Hi,
I am thinking of using this code to output and readin the data:
------------
File thefile = new File(outfile);
FileOutputStream fout = new FileOutputStream(thefile);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(vectorDB);
oos.close();
fout.close();
----------
the "vectorDB" is a ArrayList, with each element (item) of it another ArrayList.

Anyone can confirm that usign the above code, will it be properly saved to
file? (in another word, does writeObject can handle nested objects?)

Yes, writeObject() can handle nested objects (as long as all objects implement
Serializable). Your code will work.
 
H

hiwa

Hi,
I am thinking of using this code to output and readin the data:

------------
File thefile = new File(outfile);
FileOutputStream fout = new FileOutputStream(thefile);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(vectorDB);
oos.close();
fout.close();
----------
the "vectorDB" is a ArrayList, with each element (item) of it another ArrayList.

Anyone can confirm that usign the above code, will it be properly saved to
file? (in another word, does writeObject can handle nested objects?)

Thanks a lot. :)

Try youself and report the result on the forum.
Try XMLEncoder/Decoder instead of ObjectOutput/InputStream.
Or, throw away raw Java object and use ordinary DB.
 
A

Andrew Thompson

Kaidi said:
Hi,
I am thinking of using this code to output and readin the data:
<snip 'snippet'>

Java object serialisation is quite magical,
serialize a vetor of your custom object,
containing many to many links between
the objects, and you'll find they deserialize
into the same structure.

Great stuff. Try it.
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top