Infolinks In Text Ads

Oct 21, 2010

Serialization In Java

Serialization In Java -
 In java, serialization is the process for writing the state of object in sequence
 of bytes stream. Serialization process for an object also called marshalling.

 How to serialize an object?
     Serializble is a marker interface, Through the implimantaion of Serializble interface
 any object can be serialize.

 There is no methods in Serializble interface, but Serializble interface tell to JVM
 that Object can be written in byte stream.

 If Serialized object have any transient variable. It means transient variable will not
 written in to byte stream.

 Example of Serialization -

 class A implements Serializable{
   
    A(String personName, Integer age){
        this.personName=personName;
        this.age=age;
    }
    private String personName = null;
    private transient Integer age = null;
   
    public Integer getAge() {
        return age;
    }
    public String getPersonName() {
        return personName;
    }
}

public class Test {
    public static void main(String[] args) {
        try{
            A a = new A("Alber", new Integer(50));
            ObjectOutputStream ObjOut = new ObjectOutputStream(new FileOutputStream("test.txt"));
            ObjOut.writeObject(a);
            ObjOut.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...