Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 36: Share Java objects using e-mail

The Serializable interface, new with JDK 1.1, simplifies object persistence. Here's how to transfer an object to another user via SMTP e-mail

  • Print
  • Feedback

Page 2 of 6

Serialize objects

Version 1.1 of the JDK provides a wonderful mechanism for serializing and reconstituting Java objects: the java.io.Serializable interface. This interface gives your applet access to methods for "saving" objects (writeObject()) and restoring them (readObject()). In many cases, using this interface is as easy as implementing it and calling these two methods.

The following code fragment defines a (very) simple BugReport object that implements the serializable interface at its easiest.

 
 1   import java.io.*;
 2    public class BugReport implements Serializable {
 3      private Float  m_SoftwareVersion;    // version number from Help.About, e.g. "1.0"
 4      private String m_ErrorDescription;   // Description of error
 5      private int    m_Severity;           // 1=System unusable - 5=Minor Aesthetic defect

6 public BugReport (Float SoftwareVersion, String ErrorDescription, int Severity) { 7 m_SoftwareVersion = SoftwareVersion; 8 m_ErrorDesctiption = ErrorDescription; 9 m_Severity = Severity; 10 }
11 public BugReport () {} // for reconstituting serialized objects
12 public void save (OutputStream os) 13 throws IOException { 14 try { 15 ObjectOutputStream o = new ObjectOutputStream(os); 16 o.writeObject(this); 17 o.flush(); 18 } 19 catch (IOException e) {throw e;} 20 }
21 public BugReport restore (InputStream is) 22 throws IOException, ClassNotFoundException { 23 BugReport RestoredBugReport = null; 24 try { 25 ObjectInputStream o = new ObjectInputStream(is); 26 RestoredBugReport = (BugReport)o.readObject(); 27 } 28 catch (IOException e) {throw e;} 29 catch (ClassNotFoundException e) {throw e;} 30 return RestoredBugReport; 31 } 32 }


Here's what the above code does:

Line Description


1 Imports references to the I/O package, including the Serializable interface.


2-5 Defines the member variables of the class and indicates that this class implements the Serializable interface.


6-10 Provides a simple constructor.


11 A constructor that creates a "blank" bug report. This constructor is used when we reconstitute a serialized object. See example below.


12-20 Defines a method for writing the object to an already open OutputStream. The first thing this method does is create an ObjectOutputStream from the OutputStream object that was passed in by the calling program unit. It then invokes the writeObject() method and explicitly flushes the output stream prior to returning to the caller.


21-30 Defines a method for reading a BugReport object from an already opened InputStream. Note that the readObject() method can throw an ObjectNotFoundException exception if the next object encountered on the input stream is not of the same class as the object into which it is being read.


Using the BugReport object is really simple. Suppose we wanted to create a new BugReport and save it to a file. Here is a code fragment that we could use:

 1   import java.io.*;
     :
 2   BugReport bug = new BugReport(1.0, "Crashes when spell checker invoked", 2);
 3   FileOutputStream os = new FileOutputStream("MyBug.test");
 4   bug.save(os);


Easy, right? Of course, once the object has been serialized, nothing stops you from further manipulating the state of the object. The file created by the example above will only contain a copy of the object as it existed when it was written to disk. Therefore you have to be sure you don't inadvertently lose changes to the state of the object by failing to serialize it after all changes have been made.

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources