Java Reference
In-Depth Information
When bytes encoding a serialized graph of objects are read by the meth-
od readObject of ObjectInputStream that is, deserialized the result is a graph
of objects equivalent to the input graph.
Suppose, for example, that you have a HashMap object that you wish to
store into a file for future use. You could write the graph of objects that
starts with the hash map this way:
FileOutputStream fileOut = new FileOutputStream("tab");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
HashMap<?,?> hash = getHashMap();
out.writeObject(hash);
As you can see, this approach is quite straightforward. The single
writeObject on hash writes the entire contents of the hash map, including
all entries, all the objects that the entries refer to, and so on, until the
entire graph of interconnected objects has been visited. A new copy of
the hash map could be reconstituted from the serialized bytes:
FileInputStream fileIn = new FileInputStream("tab");
ObjectInputStream in = new ObjectInputStream(fileIn);
HashMap<?,?> newHash = (HashMap<?,?>) in.readObject();
Serialization preserves the integrity of the graph itself. Suppose, for ex-
ample, that in a serialized hash map, an object was stored under two
different keys:
 
Search WWH ::




Custom Search