码迷,mamicode.com
首页 > 其他好文 > 详细

对象序列化

时间:2016-01-09 17:00:41      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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();
    }
    
}

 

对象序列化

标签:

原文地址:http://www.cnblogs.com/lovedaydream/p/5116637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!