Serialization is the process of saving an object in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form. The serialized objects are JVM independent and can be re-serialized by any JVM. In this case the "in memory" java objects state are converted into a byte stream. This type of the file can not be understood by the user. It is a special types of object i.e. reused by the JVM (Java Virtual Machine). This process of serializing an object is also called deflating or marshalling an object.
The given example shows the implementation of
serialization to an object. This program takes a file name that is machine
understandable and then serialize the object. The object to be serialized must
implement java.io.Serializable class.
Default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields.
class ObjectOutputStream extends java.io.OutputStream implements ObjectOutput,
ObjectOutput interface extends the DataOutput interface and adds methods for serializing objects and writing bytes to the file. The ObjectOutputStream extends java.io.OutputStream and implements ObjectOutput interface. It serializes objects, arrays, and other values to a stream. Thus the constructor of ObjectOutputStream is written as:
ObjectOutput ObjOut = new ObjectOutputStream(new FileOutputStream(f));
Above code has been used to create the instance of the ObjectOutput class with the ObjectOutputStream( ) constructor which takes the instance of the FileOuputStream as a parameter.
The ObjectOutput interface is used by implementing the ObjectOutputStream class. The ObjectOutputStream is constructed to serialize the object.
Method writeObject() writes an object to the given stream.
Here is the code of program:
Output of the Program:
Please enter File name : Filterfile
Enter extention : txt
Serializing an Object Creation is completly Successfully.
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles