Home Java Example Java Io Serializing an Object in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Serializing an Object in Java

Advertisement
In this section, you will learn how to Serialize 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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: Serializing an Object in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:7
kalai
May 28, 2011
Java programmer

hi roseindia.net very good example..... I really appreciate that@!!! Keep it up......
girija
May 29, 2011
comment

your example and explanation about the topic was soo nice, thank you
girija
May 29, 2011
comment

i dont know what is non-transient and non-static fields ?
Bhaskar Garai
June 2, 2011
The Above Output

"Serializing an Object Creation is completly Successfully." this is a wrong output. The output should be: "Serializing an Object Creation completed successfully."
imteyaj
October 31, 2011
serialization

this is very useful for learner
Booma devi.T
August 22, 2012
wishing to your answers

this answer is super, it's understanding easily it's very well
Suraj
December 18, 2012
For multiple objects??

How to run a loop for multiple objects to serialize and deserialize
DMCA.com