Serializing an Object in Java

In this section, you will learn how to Serialize an Object in java.

Serializing an Object in Java

Serializing an Object in Java

     

Introduction

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. 

writeObject():

Method writeObject() writes an object to the given stream.

Here is the code of program:

import java.io.*;

  public class SerializingObject{
 
  
public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter File name : ");
 
  
String file = in.readLine();
 
 
System.out.print("Enter extention : ");
 
  
String ext = in.readLine();
  
  
String filename = file + "." + ext;
 
 
File f = new File(filename);
  
  
try{
 
 
ObjectOutput ObjOut = new ObjectOutputStream(new FileOutputStream(f));
 
 
ObjOut.writeObject(f);
  
  
ObjOut.close();
 
 
System.out.println(
  
"Serializing an Object Creation completed successfully.");
 
  
}
  
catch(IOException e){
 
 
System.out.println(e.getMessage());

    }
  
}


  }


Output of the Program:

C:\nisha>javac SerializingObject.java

C:\nisha>java SerializingObject
Please enter File name : Filterfile
Enter extention : txt
Serializing an Object Creation is completly Successfully.

C:\nisha>

Download this example.

CLICK HERE TO SEE THE SERIALIZATION & DESERIALIZATION COMPLETE TUTORIAL