Serialization
Serialization
Using Serialization
Serialization is a mechanism built into the core Java libraries for writing a graph of objects into a
stream of data. This stream of data can then be programmatically manipulated, and a deep copy
of the objects can be made by reversing the process. This reversal is often called deserialization.
As a persistence mechanism
If the stream being used is FileOutputStream, then the data will automatically be
written to a file.
As a copy mechanism
If the stream being used is ByteArrayOutputStream, then the data will be written to a
byte array in memory. This byte array can then be used to create duplicates of the original
objects.
As a communication mechanism
If the stream being used comes from a socket, then the data will automatically be sent
over the wire to the receiving socket, at which point another program will decide what to
do.
If we have a serializable class, we can save it to a file or make a copy of it simply by changing
the way we use the output of the serialization mechanism.