Java Serialization Overview
22 April 2007Serialization is the process of saving the state of an object onto any storage medium. Storage medium can be a file or a memory buffer. Then you can transmit it across a network, either in binary form, or in some human-readable text format such as XML. The object in the form of bytes or any other format can be recreated to its internal state easily. The process of serializing an object is also called “ Marshalling ” an object. Extracting a data structure from a series of bytes, is “ Deserialization ” or “ Unmarshalling ”.
Java of course provides serialization which requires only that the object be marked by implementing the java.io.Serializable interface. Implementing the interface marks the class as serialized and Java then handles serialization internally. This interface Serializable has no methods. It simply serves to indicate that the implementing class may be serialized.Listed below is an example to show “ Serialization ”.
Class MySerialObject has two private attributes name and age which are initialized in the default constructor. The objective is to serialize an object of this class (Note that MySerialObject class is implementing Serializable interface). CustomDataExample class create instances of MySerialObject that will be serialized. Attribute “ filePath ” contains the path and name of the file where we will store our object.
FileOutputStream fos = new FileOutputStream( filePath ); ObjectOutputStream outStream = new ObjectOutputStream( fos );
Finally “ save ” the object and call “ flush ” method for the object, which forces the data to get written to stream.
Now we come to reading part. Create stream for reading and then create an object that can read from that file.
FileInputStream fis = new FileInputStream( filePath ); ObjectInputStream inStream = new ObjectInputStream( fis );
Retrieve the serialized object and display its contents using “ displayAttrs() ” method.
public class CustomDataExample { String filePath; public static void main( String args[] ) { CustomDataExample cde = new CustomDataExample(); } CustomDataExample() { try { MySerialObject serialObject = new MySerialObject(); filePath = "serial.tmp"; FileOutputStream fos = new FileOutputStream( filePath ); ObjectOutputStream outStream = new ObjectOutputStream( fos ); // Save the object. outStream.writeObject( serialObject ); outStream.flush(); FileInputStream fis = new FileInputStream( filePath ); ObjectInputStream inStream = new ObjectInputStream( fis ); serialObject = ( MySerialObject )inStream.readObject(); serialObject.displayAttrs(); } catch ( Exception e ) { e.printStackTrace(); } } } import java.io.*; public class MySerialObject implements Serializable { private int age; private String name; MySerialObject() { age = 25; name = new String( "Lara" ); } public void displayAttrs() { System.out.println("name = " + name); System.out.println( "age = " + age); } } Output: name = Lara age = 25
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Java Serialization Overview