Serialize.javaFrom WikiJava
the article
A class, to be serializable must implement the interface like this: public class Serialize implements Serializable The When serializing an object the Serialization API, recursively tries to serialize also all the objects contained in it. This means that also all the fields of the class must be serializable, otherwise you will get the If you really need to have not serializable classes (the ones which don't implement Being the
it can be serialized without problems. The way to serialize it is to use the method: void java.io.ObjectOutputStream.writeObject(Object obj) throws IOException In the example: oos.writeObject(toExport); Which writes the object To read the object from the file (deserialize) we use: Object java.io.ObjectInputStream.readObject() throws IOException, ClassNotFoundException In the example: Object object = ois.readObject(); Serialize imported = (Serialize) object; Which reads an Object from the Note that (as for Java 6) the generics haven't arrived to the point of relieving us from having to perform manually the cast. This would have been anyway quite complex, we hope that the future versions of Java will extend to this point. serialize.javapackage org.wikijava.serialization; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Serialize implements Serializable { private static final long serialVersionUID = 1L; /** * * 1: creates a serializable object, 2: writes it to a file on the disk, 3: * reads the written file 4: outputs the object created from the file * * @param args * @throws FileNotFoundException * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Serialize toExport = new Serialize(); String filename = "serializedVersion"; // writes the object to file writeToFile(toExport, filename); // reads a new object from file Serialize imported = readFromFile(filename); // output results. System.out.println("serializableField : " + imported.getSerializableField()); } /** * loads the object from a file * * @param filename * the filename containing the object * @return the imported object * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */ private static Serialize readFromFile(String filename) throws IOException, FileNotFoundException, ClassNotFoundException { // open the file FileInputStream fis = new FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); // read the object Object object = ois.readObject(); Serialize imported = (Serialize) object; // close the file ois.close(); return imported; } /** * saves the object to a file * * @param toExport * the object to export * @param filename * the filename to export to * @throws IOException * @throws FileNotFoundException */ private static void writeToFile(Serialize toExport, String filename) throws IOException, FileNotFoundException { // open the file FileOutputStream fos = new FileOutputStream(filename); ObjectOutputStream oos = new ObjectOutputStream(fos); // write the object oos.writeObject(toExport); oos.flush(); // close the file oos.close(); } private String serializableField; /** * CONSTRUCTOR */ public Serialize() { this.serializableField = "serializableString"; } public String getSerializableField() { return this.serializableField; } public void setSerializableField(String serializableField) { this.serializableField = serializableField; } } |
