----------------------------------------------------------------------- 0) Serialization + SERIALIZATION - reading and writing of objects called "object serialization" - allows objects to communicate with sockets - allows objects to be saved for use in a later invocation of a program - uses the ObjectInputStream and ObjectOutputStream classes to read and write objects http://java.sun.com/docs/books/tutorial/essential/io/serialization.html ----------------------------------------------------------------------- 1) How to Write to an ObjectOutputStream + WRITING TO THE ObjectOutputStream - The ObjectOutputStream constructor takes an output stream as a parameter - create it just like any other stream - ObjectOutputStream has the special method writeObject() to write objects to the stream - This example (from the Java Tutorial) writes a String and a Date object to a file: FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject("Today"); s.writeObject(new Date()); s.flush(); - the writeObject() method serializes the object, and serializes any objects pointed to by an object being serialized - *** An object must implement the Serializable interface to be serializable. http://java.sun.com/docs/books/tutorial/essential/io/serializing.html ----------------------------------------------------------------------- 2) How to Read from an ObjectInputStream + READING FROM THE ObjectInputStream - just like reading from any other stream - has the special method readObject(), which reads and returns the object from the stream - This example reads back in the objects written in the previous example FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject(); - ObjectInputStream must be constructed on another stream (an input stream), just like ObjectOutputStream is constructed on an output stream - the readObject() method deserializes the object and in the stream and recursively deserializes all objects reachable from the current object. This maintains all relationships between objects http://java.sun.com/docs/books/tutorial/essential/io/serializing.html ----------------------------------------------------------------------- 3) Providing Serialization for your Objects + SERIALIZABILITY - object must implement the Serializable interface to be serializable - the Serializable interface has no methods; it just identifies serializable objects. Thus just add "implements Serializable" to your class definition - here's an example: public class MySerializableClass implements Serializable { ... } + HOW ARE OBJECTS SERIALIZED/DESERIALIZED? - the defaultWriteObject() method of ObjectOutputStream handles serialization - the defaultReadObject() method of ObjectInputStream handles deserialization - defaultWriteObject() writes out everything required to reconstruct the object, including: - class of the object - class signature - values of all non-transient and non-static members, including members that refer to other objects http://java.sun.com/docs/books/tutorial/essential/io/providing.html