java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to do object Serialization E-mail
User Rating: / 29
PoorBest 

Object Serialization transform an object into a sequence of bytes so it cam be written to disc or can be transfer to the server. This Sequence of bytes can be later deserialized into an original object. After deserialization, the object has the same state as it had when it was serialized

Java provides this facility through ObjectInput and ObjectOutput interfaces. The concrete implementation of ObjectOutput and OnjectInput interfaces is provided in ObjectOutputStream and ObjectInputStream classes respectively. These two interfaces have the following methods:

   final void writeObject(Object obj) tthrows IOException
   final Object readObejct() through IOException, ClassNotFoundException

The writeObject() method can be used be used to write any object to a stream, including strings and arrays and readObject() method can be used to read any stream as long as an object supports java.io.Serializable interface.

This code first writes the serializable objects to a file. And then read the same file and prints the object on the console. While writing to the file the objects are converted to the byte streams and while reading this byte stream is de serialized to get the objects in the original state.

import java.io.*;

public class SerializableEmployee implements Serializable {
    String name;
    int age;
    double salary;

    SerializableEmployee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public void showDetails() {
        System.out.println("Name   : " + name);
        System.out.println("Age    : " + age);
        System.out.println("Salary : " + salary);
    }
}

class ObjectSerializationDemo {
    void writeData() {
        SerializableEmployee db[] {
                new SerializableEmployee("Tom"251200.25),
                new SerializableEmployee("Dick"221223.25),
                new SerializableEmployee("Harry"251345.25) };
        try {
            FileOutputStream out = new FileOutputStream("c://employee.txt");
            try {
                ObjectOutputStream sout = new ObjectOutputStream(out);
                for (int i = 0; i < db.length; i++) {
                    sout.writeObject(db[i]);
                }
                sout.close();

            catch (Exception e) {
                        e.printStackTrace();
        }
    }
     void readData() {
        try {
            FileInputStream in = new FileInputStream("c://employee.txt");
            try {
                ObjectInputStream sin = new ObjectInputStream(in);
                try {
                    SerializableEmployee se = (SerializableEmployeesin
                            .readObject();
                    se.showDetails();
                    se = (SerializableEmployeesin.readObject();
                    se.showDetails();
                    se = (SerializableEmployeesin.readObject();
                    se.showDetails();

                    sin.close();

                catch (Exception e) {
                        e.printStackTrace();
                }
        }

    public static void main(String[] args) {
        ObjectSerializationDemo impl = new ObjectSerializationDemo();
        impl.writeData();
        impl.readData();

    }

}

Output:

Name   : Tom
Age    : 25
Salary : 1200.25
Name   : Dick
Age    : 22
Salary : 1223.25
Name   : Harry
Age    : 25
Salary: 1345.25

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.