ObjectInputStream and ObjectOutputStream allows you to read and write objects from any class (not just primitive types).
|
Java objects are serialized with writeObject() and deserialized with readObject(). For example:
-
Vector lines = new Vector( 256 ); ...
-
try {
-
new ObjectOutputStream(
-
new GZIPOutputStream(
-
new FileOutputStream( filename ) ) );
-
out.writeObject( lines );
-
out.close();
-
} catch ( IOException e ) { }
|
Only objects of classes that implement Serializable (or Externalizable) can be serialized. (The Serializable interface defines no methods.)
|
Object variables not to be serialized are called transient.
|