0

Write now I have this class that I would like to be able to save and open using serialization:

public class Region implements Serializable
{
private final int      inputNumberOfColumnsAlongXAxis;
private final int      inputNumberOfColumnsAlongYAxis;
private double         inputDataScaleReductionOnXAxis;
private double         inputDataScaleReductionOnYAxis;

private int            numberOfColumnsAlongXAxis;
private int            numberOfColumnsAlongYAxis;
private int            cellsPerColumn;                // Z-Axis dimension
private float          inhibitionRadius;
private final float    percentMinimumOverlapScore;
private final float    minimumOverlapScore;

I've never done object serialization before, so any help would be greatly appreciated!

4 Answers 4

2

The simplest thing you need to do is add a private static field to your class with the name serialVersionUID. For instance:

private static final long serialVersionUID = 1L;

This is used by the default serialization mechanism to match up class names and formats.

You can then write instances of your object to an ObjectOutputStream and read them back from an ObjectInputStream:

Region r = . . .;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(r);
oos.close();
ObjectInputStream ois = new ObjectInputStream(
    new ByteArrayInputStream(bos.getBytes()));
Region r2 = (Region) ois.readObject();
// voilà - a very expensive clone()!

For more control over your object serialization, you can implement these methods:

private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;
 private void readObjectNoData() 
     throws ObjectStreamException;

You then have total control over the serialization of your objects. For more info, see the docs on Serializable.

UPDATE: Strictly speaking, you don't need to declare serialVersionUID; the runtime environment will calculate one for you automatically if it's missing. However, the docs have this to say about it (emphasis in the original):

However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.

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

Comments

0

you would not need to do anything there, because all the Region attributes are primitive types i.e. serializable. Marking your class as Serializable will already do the trick.

UPDATE: sorry I missed it, you need to add the serialVersionUID.

Comments

0

There is a ton of documentation and a ton of examples on how to do Object serialization in Java on the internet. You are on the right track by implementing the Serializable interface.

To actually serialize the class, look at ObjectOutputStream.

Comments

0

checkout this link for sample code: http://www.tutorialspoint.com/java/java_serialization.htm

consider to use jaxb library (part of java 6 and up) you can serialize to xml giving you a human readable java in depended format.

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.