对象序列化

import java.io.*;
class Person implements Serializable
{
    String name ="wang";
    transient int age=8;
    static String country="cn";
    Person(String name,int age,String country)
    {
        this.age = age;
        this.name = name;
        this.country = country;
    }
    public  String toString()
    {
        return "name:"+name+"age:"+age+"country:"+country;
    }
}
import java.io.*;
class ObjectStreamDemo
{
    public static void main(String[] args)throws Exception
    {
        //writeObj();
        readObj();
    }
    
    public static void readObj()throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Person.object"));
        Person p = (Person)ois.readObject();
        System.out.println(p);
    }    
    
    public static void writeObj()throws Exception
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Person.object"));
        oos.writeObject(new Person("lisi",24,"fr"));
        oos.close();
    }
    
}

 

posted @ 2016-01-09 16:06  lovedaydream  阅读(104)  评论(0编辑  收藏  举报