JAVA CODE FOR SERIALIZATION AND DESERIALIZATION: PERSON DETAILS | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JAVA CODE FOR SERIALIZATION AND DESERIALIZATION: PERSON DETAILS



PROGRAM:
import java.io.*;
class PersonMain
{
            public static void main(String arg[])throws IOException
            {
                        Person p1=new Person("MARTIN”,21,"LONDON");
                        System.out.println("object1:"+p1);
                        FileOutputStream fos=new FileOutputStream("person.txt");
                        ObjectOutputStream oos=new ObjectOutputStream(fos);
                        oos.writeObject(p1);
                        Person p2=new Person("SMITH”,23,"PARIS");
                        System.out.println("object2:"+p2);
                        oos.writeObject(p2);
                        oos.close();
            }
}
class Person implements Serializable
{
            String name;
            int age;
            String add;
            Person(String a, int d,String ad)
            {
                        name=a;
                        age=d;
                        add=ad;
            }
            public String toString()
            {
            return "\nName:"+name"\nAge:"+age+"\nAddress:"+add;
            }
  }

OUTPUT:

object1:
Name:MARTIN
            Age:21
Address: LONDON
object2:
Name:SMITH
 Age:23

Address:PARIS

No comments:

Post a Comment

Thanks for your valuable comments

Followers