tags:

views:

94

answers:

7

e.g I have an Object obj,how can I save all its information such as m.getName();m.getFamily() and the others in the file???

+1  A: 

You may be after serialization.

Smashery
+1  A: 

since xml is more readable and transferable, i use Simple Framework, or as mentioned you could do simple serialization.

pstanton
you could also us Xstream.
Milhous
+5  A: 

Using an ObjectOutputStream you can write the object directly into the file.

 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("filename.dat")));
 out.writeObject(obj);
 out.close();

This writes the Object obj out to the file. It can be read back in use an ObjectInputStream().

Vincent Ramdhanie
This technique can also be used to send arbitrary objects over a socket stream.
Ben S
A: 

If you just need to store the object and retrieve it back as a whole , better go with serialization.

In case, you want a readable file with the contents of the object , use file-IO API to write the contents to a file.

Nrj
A: 

Yet another answer, but Apache Commons Lang has SerializationUtils

SerializationUtils.serialize(Serializable obj, OutputStream outputStream).
John Paulett
+1  A: 

My workplace uses XStream, if ObjectOutputStream doesn't do the job.

Chip Uni
A: 

You can use Xstream to write objects to xml files.

Milhous