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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top