Rule #1: The object to be persisted must implement the Serializable
interface or inherit that implementation from its object hierarchy.
Rule #2: The object to be persisted must mark all nonserializable fields transient
persist the object. That is done with the java.io.ObjectOutputStream
class. That class is a filter stream--it is wrapped around a lower-level byte stream (called anode stream) to handle the serialization protocol for us. Node streams can be used to write to file systems or even across sockets. That means we could easily transfer a flattened object across a network wire and have it be rebuilt on the other side!
180 fos = new FileOutputStream(filename);
190 out = new ObjectOutputStream(fos);
200 out.writeObject(time);
190 fis = new FileInputStream(filename);
200 in = new ObjectInputStream(fis);
210 time = (PersistentTime)in.readObject();
ObjectOutputStream.reset()
java.io.InvalidClassException
serialVersionUID
No comments:
Post a Comment