Java Reference
In-Depth Information
This clears the record that is kept within the stream object of what has been written and writes a 'reset
marker' to the stream. When an ObjectInputStream object reads a 'reset marker' it too clears its
record of what has been read, so that subsequent object read operations will be as if the stream started
at that point. To make effective use of this, your code will clearly need to accommodate the possibility
of multiple versions of the same object existing in the stream. It's your code, so you will know what you
want to do. To make our example work as expected, we can reset the stream before each output
operation after the first call to writeObject() , like this:
objectOut.writeObject(data); // Write object
System.out.println("1st Object written has value: "+data.getValue());
data.setValue(2); // Modify the object
objectOut.reset();
objectOut.writeObject(data); // and write it again
System.out.println("2nd Object written has value: "+data.getValue());
data.setValue(3); // Modify the object again...
objectOut.reset();
objectOut.writeObject(data); // and write it once more
System.out.println("3rd Object written has value: "+data.getValue());
If you insert the calls to reset() in the original code and run the example again, you should get the
output you were expecting.
A further complication arises with serialized objects when you change the definition of a class in some
way. When an object is written to a file, part of the information identifying the class is a sort of
hashcode, called a version ID , that is intended to ensure that the definition of the class used when you
are reading an object from a file is the same as the class definition that was used when the object was
written. Even cosmetic changes between writing and reading a stream, such as changing the name of a
field, can alter the version ID, so in this case a read operation will fail with an
InvalidClassException being thrown. In general, you need to make sure that the class definitions
in a program reading a file are the same as those used when the file was written, although you can
explicitly set the version number and deal with any changes yourself.
For more complex situations, it is possible to take complete control of the serialization process within
your classes by implementing the Externalizable interface. This is important when the class
definition for the object involves change over time. With careful programming you can accommodate
modifications to classes without invalidating existing serialized objects. A detailed discussion of what is
involved in this is outside the scope of this topic.
Summary
In this chapter we have explored how we can write objects to a file and read them back. Making your
class serializable makes it very easy to save your application data in a file. While what we have
discussed is by no means exhaustive, you now know enough to deal with straightforward object
serialization. The important points in this chapter are:
To make objects of a class serializable the class must implement the Serializable interface.
If a class has a superclass that does not implement the Serializable interface, then the
superclass must have a public default constructor if it is to be possible to serialize the class.
Search WWH ::




Custom Search