Object serialization is the process of saving an
object's state to a sequence of bytes, as well as the process of rebuilding
those bytes into a live object at some future time. The Java Serialization API
provides a standard mechanism for developers to handle object serialization. The
API is small and easy to use, provided the classes and methods are
understood. This hands-on lab takes you through the basics of using Java
serialization.
import
java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class SerializeTime{ public static void main(String [] args){ String filename = "time.ser"; if(args.length > 0) { filename = args[0]; } // Create an object PersistentTime time = new PersistentTime(); // Serialize the object instance and save it in // a file. FileOutputStream fos = null; ObjectOutputStream out = null; try { fos = new FileOutputStream(filename); out = new ObjectOutputStream(fos); out.writeObject(time); out.close(); } catch(IOException ex) { ex.printStackTrace(); } System.out.println("Current time is saved into " + filename); } } |
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ private Date time; public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } } |
import
java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Calendar; public class DeserializeTime { public static void main(String [] args) { String filename = "time.ser"; if(args.length > 0) { filename = args[0]; } // Deserialize the previously saved // PersistentTime object instance. PersistentTime time = null; FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream(filename); in = new ObjectInputStream(fis); time = (PersistentTime)in.readObject(); in.close(); } catch(IOException ex) { ex.printStackTrace(); } catch(ClassNotFoundException ex) { ex.printStackTrace(); } // print out restored time System.out.println("Previously serialized time: " + time.getTime()); // print out the current time System.out.println("Current time: " + Calendar.getInstance().getTime()); } } |
Current time is saved into
time.ser |
Previously serialized time: Mon Feb 26
01:57:16 EST 2007 Current time: Mon Feb 26 01:58:58 EST 2007 |
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ transient private Date time; public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } } |
Current time is saved into
time.ser |
Previously
serialized time: null Current time: Mon Feb 26 02:07:09 EST 2007 |
In this exercise, you will learn how to do version control.
Current time is saved into
time.ser |
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ private Date time; private String aNewField; public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } } |
java.io.InvalidClassException:
PersistentTime; local class incompatible: stream classdesc
serialVersionUID = -3126998878902358585, local class serialVersionUID =
-5560460247034149373 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:519) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1546) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1460) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1693) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339) at DeserializeTime.main(DeserializeTime.java:23) Exception in thread "main" java.lang.NullPointerException |
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ static final long serialVersionUID = -3126998878902358585L; private Date time; private String aNewField; public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } } |
Current time is saved into
time.ser |
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ static final long serialVersionUID = -3126998878902358585L; private Date time; private String aNewField; private String aNewNewField; public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } } |
Previously serialized time: Mon Feb 26
03:04:59 EST 2007 Current time: Mon Feb 26 03:05:13 EST 2007 |
In this exercise, you have learned how to do
version control for serialization and deserialization of an
object.
In this exercise, you will learn how to provide
your own readObject() and writeObject() methods, thus changing the behavior of
the default protocol.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeAnimationThreadNotStarted { public static void main(String[] args) { // Create an object instance PersistentAnimation a = new PersistentAnimation(1); // Serialize the object FileOutputStream fos = null; ObjectOutputStream out = null; try { fos = new FileOutputStream("serializedfile"); out = new ObjectOutputStream(fos); out.writeObject(a); out.close(); } catch(IOException ex) { ex.printStackTrace(); } // Deserialize the object. The problem is that the // PersistentAnimation thread does not get started // automatically after the deserialization since its // constructor method does not get called when the // serialized object is deserialized. This is why // the PersistentAnimation class has to have its own // readObject() method in which the thread is // explicitly started. PersistentAnimation b = null; FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream("serializedfile"); in = new ObjectInputStream(fis); b = (PersistentAnimation)in.readObject(); in.close(); } catch(IOException ex) { ex.printStackTrace(); } catch(ClassNotFoundException ex) { ex.printStackTrace(); } } } |
import
java.io.Serializable; public class PersistentAnimation implements Serializable, Runnable { transient private Thread animator; private int animationSpeed; public PersistentAnimation(int animationSpeed) { this.animationSpeed = animationSpeed; animator = new Thread(this); animator.start(); } public void run() { System.out.println("PersistentAnimation thread is started"); } } |
PersistentAnimation thread is
started |
import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class PersistentAnimation implements Serializable, Runnable { transient private Thread animator; private int animationSpeed; public PersistentAnimation(int animationSpeed) { this.animationSpeed = animationSpeed; startAnimation(); } public void run() { System.out.println("PersistentAnimation thread is started"); } // Provide your own writeObject method private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); } // Provide your own readObject method private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // our "pseudo-constructor" in.defaultReadObject(); // now we are a "live" object again, so let's run rebuild and start startAnimation(); } private void startAnimation() { animator = new Thread(this); animator.start(); } } |
PersistentAnimation thread is
started PersistentAnimation thread is started |