1

I have a large amount of data stored in a Collection.
I would like to save this data to a file. Is it a good idea to use Serialization?
Or should I use a custom format to save the data, or save it as XML for example?
(The elements in the Collection are custom classes. Do I need to implement a method that serializes the objects?)

3 Answers 3

1

You can use both methods. I would prefer to save them as XML, because it is less likely to have a data corruption in XML file. But if you want to save custom class into data file using serialization you need to implement Serializable interface in those custom classes.

Sign up to request clarification or add additional context in comments.

5 Comments

Serializable classes are not guaranteed to be de-Serializable in the next Java version or micro-version. For that reason XMLEncoder & XMLDecoder are a more robust method of storing and retrieving the data. To use the XML based classes, the objects of interest will require a bean like form.
Why would using serialisation make it more likely to lead to data corruption?? In fact XML limits text to a subset of characters, unless you base64 encode, or similar.
@Andrew Thompson You are thinking of Swing. Serialisation in AWT wasn't well thought out. XMLEncoder and XMLDecoder are a hack (look in the source code, and see all the hacks to handle standard classes in other packages).
@Tom Hawtin. I never mentioned data corruption. My understanding is that the data might be serialized in different forms according to different JVMs. And how did all the discussion of AWT/Swing come into it? I was referring to POJOs.
@Andrew Thompson My original comment refers to the answer. AWT/Swing is as far as I am aware the only place in the Java library where serial compatibility is not standardised. However, computation of serialVersionUID may work out differently between compilers (unfortunately the specification for automatic generation includes synthetic method), hence the recommendation and warnings to be explicit.
0

I would not write a serialized class to disk. Once the JVM or any libraries change it might be useless. This means a file created on one system may not work on another!

Instead, I'd write a text version of the data. If your collection includes other collections or classes, I'd use XML as it handles nesting well. If the data is simple I'd probably just write a comma-sep file with a header line including a version number and a description of the data set, a line telling the column names, the data lines, and an EOF line.

8 Comments

"This means a file created on one system may not work on another!" What??
That's right. Serialized (not XML'ed or what have you - serialized) is dependent on individual jar versions on each system. Suppose you serialize a class that contains an int. Then it gets de-serialized on a system where that class has a two ints - what happens? I ran into this very thing years ago when I was using RMI to pass serialized objects from system to system.
Here's an SO thread on this very thing. I can't remember the details now, it's been 10 years or more, but 'serialVersionUID' rings a bell. stackoverflow.com/questions/1576703/…
@Tom Hawtin I just learned if you "@" someone's nickname they get a notification :-)
@Tony Ennis Make sure your colleagues' other colleague warns them. :)
|
0

Do I need to implement a method that serializes the objects?

In order to serialize an object you should implement the Serializable interface OR provide the implementation for the following methods IF a 'special'handling during the serialization and deserialization process is required.

- private void writeObject(ObjectOutputStream out) throws IOException;
- private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

You can find more details on serialization on the oracle site. You can visit http://java.sun.com/developer/technicalArticles/Programming/serialization/ to get started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.