001package serialization; 002import java.io.*; 003 004public class Main1 { 005 public static void main(String args[]) { 006 try { 007 ObjectOutputStream os 008 = new ObjectOutputStream (new FileOutputStream("out.dat")); 009 os.writeObject(new Entry("Save Me", 1)); 010 os.close(); 011 012 ObjectInputStream is 013 = new ObjectInputStream (new FileInputStream("out.dat")); 014 Object o = is.readObject(); 015 is.close(); 016 017 Entry e = (Entry) o; 018 System.out.println("Entry restored from file is: " + e.toString()); 019 } catch (Exception e) { e.printStackTrace(); } 020 } 021} 022 023class Entry implements Serializable { 024 private static final long serialVersionUID = 2008L; 025 private String message = ""; 026 private int messageNumber = 0; 027 028 public Entry(String message, int messageNumber) { 029 this.message = message; 030 this.messageNumber = messageNumber; 031 } 032 public String getMessage() { 033 return message; 034 } 035 public int getMessageNumber() { 036 return messageNumber; 037 } 038 public String toString() { 039 return message + " " + Integer.toString(messageNumber); 040 } 041}