Serialization
in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
ObjectOutputStream
maintains a mapping from
instances and classes to handles.
writeObject( )
is passed an instance that has
not yet been written to the stream the instance is assigned a
reference handle, the handle is written to the stream, and the
instance data is written to the stream.
writeObject()
is called with an argument that has
already been written to the stream, the handle is written to the
stream, and no further operations are necessary.
Date d; FileOutputStream out; ObjectOutputStream serializer; String s; out = new FileOutputStream("objects.dat"); serializer = new ObjectOutputStream(out); s = new String("Today"); d = new Date(); serializer.writeObject(s); serializer.writeObject(d); serializer.flush(); out.close();
Date d; FileInputStream in; ObjectInputStream deserializer; String s; in = new FileInputStream("objects.dat"); deserializer = new ObjectInputStream(in); s = (String)deserializer.readObject(); d = (Date)deserializer.readObject(); in.close();
import java.io.Serializable; /** * A Course * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class Course implements Serializable { private static final long serialVersionUID = 1L; private int number; private String department, title; /** * Explicit Value Constructor * * @param department The department code * @param number The course number * @param title The course title */ public Course(String department, int number, String title) { this.department = department; this.number = number; this.title = title; } /** * Get the course designation * * @return The course designation */ public String getDesignation() { return department+number; } /** * Get the course title * * @return The course title */ public String getTitle() { return title; } }
import java.io.Serializable; import java.util.Iterator; import java.util.HashMap; /** * A database of Course objects * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class CourseDatabase implements Serializable { private static final long serialVersionUID = 1L; private HashMap<String, Course> db; /** * Constructor */ public CourseDatabase() { db = new HashMap<String,Course>(); } /** * Add a Course * * @param id The identifier * @param course The Course to add */ public void add(String id, Course course) { db.put(id, course); } /** * Get the values (i.e., Course objects) in the database * * @return The values */ public Iterator<Course> iterator() { return db.values().iterator(); } /** * Get a Course * * @param department The department identifier * @param number The course number * @return The Course to get */ public Course get(String department, int number) { return db.get(department+number); } /** * Remove a Course * * @param department The department identifier * @param number The course number */ public void remove(String department, int number) { db.remove(department+number); } }
import java.io.*; /** * An example of serialization * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class SerializationDriver { public static void main(String[] args) { Course cross; CourseDatabase db; FileOutputStream out; ObjectOutputStream serializer; cross = new Course("CS/MATH",227,"Discrete Mathematics"); db = new CourseDatabase(); db.add("CS139", new Course("CS",139,"Algorithm Development")); db.add("CS227", cross); db.add("CS240", new Course("CS",240,"Data Structures and Algorithms")); db.add("MATH227", cross); try { out = new FileOutputStream("objects.dat"); serializer = new ObjectOutputStream(out); serializer.writeObject(db); serializer.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
import java.io.*; import java.util.*; /** * An example of deserialization * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class DeserializationDriver { public static void main(String[] args) { Course course; CourseDatabase db; Iterator<Course> i; FileInputStream in; ObjectInputStream deserializer; try { in = new FileInputStream("objects.dat"); deserializer = new ObjectInputStream(in); db = (CourseDatabase)deserializer.readObject(); in.close(); i = db.iterator(); while (i.hasNext()) { course = i.next(); System.out.println(course.getDesignation()+"\t"+ course.getTitle()); } } catch (Exception ex) { ex.printStackTrace(); } } }
transient
private void readObject(ObjectInputStream stream)
private void writeObject(ObjectOutputStream stream)
writeObject()
and readObject()
methods that always throw the NotSerializableException
ObjectOutputStream
which will abort the serialization
process.
writeReplace()
and
readReplace()
methods