E.3. Using the serialization API

To convert an object to serializable form, a standard approach is to create an ObjectOutputStream and write the object to it. An ObjectOutputStream can be constructed on any object of a class that extends OutputStream. For example, if I wanted to serialize an object onto a network socket, I could do something like this:

Socket s = new Socket (host, port); 
OutputStream os = s.getOutputStream(); 
ObjectOutputStream ois = new ObjectOutputStream (os); 
ois.writeObject (obj); 

Multiple objects can be written to the stream if required. What the writeObject method does is to write information on the output stream about the object itself (its class, for example), then write out all its instance variables. If its instance ...

Get Applied Enterprise JavaBeans™ Technology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.