Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Serializing an Object in Java 
 

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.

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

I was very useful to me and the explanation given is very clear and understandable.

Thanks alot

Posted by porkodi on Saturday, 11.15.08 @ 00:52am | #81689

i tried this program and it throws exception error as Exection in thread main...
It would be great if you give soliution to this!

Posted by Preetha on Monday, 04.28.08 @ 10:08am | #58034

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.