I'm having trouble getting Externalizable to work. Can you give a simple code example?
Created May 4, 2012
Tim Rohaly
The following code defines a simple Externalizable class that provides a main() method which instantiates an object, writes it to a file, restores it, then verifies that the restored object contains the same values as the original.
import java.io.*; import java.util.*; /** * This program demonstrates how to write an Externalizable class */ public class TestExternal implements Externalizable { private String comment = "A serializable data member"; private Vector collection; /** * A no-argument constructor is required for an Externalizable class */ public TestExternal() { this(null); } public TestExternal(Vector collection) { this.collection = collection; } /** * This method is responsible for saving the * entire state of the object */ public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(comment); // // You could also just do out.writeObject(collection) below, // but I prefer to loop over the contents and write them one // at a time. // out.writeInt(collection.size()); Enumeration e = collection.elements(); while (e.hasMoreElements()) { out.writeObject(e.nextElement()); } } /** * This method is responsible for restoring the * entire state of the object */ public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException { // // What was written in writeExternal() must be read back here. // comment = (String) in.readObject(); int size = in.readInt(); collection = new Vector(size); for (int i=0; i<size; i++) { collection.addElement(in.readObject()); } } /** * Delegates equality check to the objects stored within * this instance */ public boolean equals(Object o) { if (o instanceof TestExternal) { TestExternal other = (TestExternal) o; if (comment.equals(other.comment) && collection.size() == other.collection.size() ) { for (int i=0; i<collection.size(); i++) { if (!collection.elementAt(i).equals(other.collection.elementAt(i))) return false; } return true; } } return false; } public static void main(String[] args) throws IOException, ClassNotFoundException { // // Create the data to store in this object, // then create and fill the object // Vector bunchOfIntegers = new Vector(); for (int i=0; i<10; i++) { Integer obj = new Integer(i); bunchOfIntegers.addElement(obj); } TestExternal original = new TestExternal(bunchOfIntegers); // // Write the object to a file // ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("testexternal.ser")); out.writeObject(original); out.flush(); out.close(); // // Now read the object back in from the file // ObjectInputStream in = new ObjectInputStream( new FileInputStream("testexternal.ser")); TestExternal restored = (TestExternal) in.readObject(); // // Compare original object with restored object to // check that they are the same // if (restored.equals(original)) { System.out.println("The original is the same as the restored"); } else { System.out.println("There was a problem with Externalization:"); System.out.println(" The original and the restored aren't equal"); } } }