[ Published on -
1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
a) Simple User entity class
package basics.basicsExamples; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; /** **************************************************************************** * SERIALIZATION EXAMPLE CLASS * * http://java.sun.com/developer/technicalArticles/Programming/serialization/ * * @author stanislav.zorjan */ public class SerializationExample { private String fileName = "serialization.ser"; private SimpleSerializedClass ssc; /** ************************************************************************ * Default constructor */ public SerializationExample() { ssc = new SimpleSerializedClass(); ssc.setString("Serialized String"); ssc.notSerializedString = "Serialize this string"; // serializing object serializeObject(ssc); // deserializing object ssc = (SimpleSerializedClass) deserializeObject(this.fileName); // reading from deserialized object System.out.println("String from deserialized object: "+ssc.getString()); System.out.println("Transient string from deserialized object: "+ssc.notSerializedString); } /** ************************************************************************ * Serialization method * @paramObject for serialization * @param * @param t */ private void serializeObject(T t){ FileOutputStream fos; ObjectOutputStream oos; try{ fos = new FileOutputStream(this.fileName); oos = new ObjectOutputStream(fos); oos.writeObject(t); oos.close(); System.out.println("Object successfully serialized"); System.out.println(""); }catch(IOException ex){ ex.printStackTrace(); } } /** ************************************************************************ * De-serialization method * @param fileName * @return */ private Object deserializeObject(String fileName){ FileInputStream fis; ObjectInputStream ois = null; Object obj = null; try{ fis = new FileInputStream(fileName); ois = new ObjectInputStream(fis); obj = ois.readObject(); }catch(IOException ex){ ex.printStackTrace(); }catch(ClassNotFoundException ex){ ex.printStackTrace(); } return obj; } } /** **************************************************************************** * SERIALIZABLE CLASS * @author stanislav.zorjan */ class SimpleSerializedClass implements Serializable,SimpleInterface { private String str; // nonserializable object/field. // when deserialized, field will have null value transient public String notSerializedString = "Not serialized string"; @Override public void setString(String value) { this.str = value; } @Override public String getString() { return this.str; } // ******** // METHODS "writeObject" and "readObject" ARE OPRIONAL. THEY ARE USED // FOR "PREPARING" OBJECT FOR PRE-SERIALIZATION/POST-DESERIALIZATION // this method is called whenever object is serialized private void writeObject(ObjectOutputStream out) throws IOException { // prepare your object // than serialize it System.out.println("prepare object for serialization"); out.defaultWriteObject(); } // this method is called whenever object is deserialized private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); // now when object is deserialized, you can "reinitialize" all actions in it System.out.println("prepare object after deserialization"); } } /** **************************************************************************** * INTERFACE INMPLEMENTED BY SERIALIZABLE CLASS * @author stanislav.zorjan */ interface SimpleInterface{ public void setString(String value); public String getString(); }