序列化与反序列化

//首先,有一个学生类

//Serializable接口是一个标示接口,相当于给类打上一个标志允许参与序列化和反序列化
public class Student implements Serializable{
    
    private String name;
    
    private int age;
    
    private Address address;
    
    public Student(){
        
    }

    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    

    public Student(String name, int age, String state,String city,String street) {
        super();
        this.name = name;
        this.age = age;
        this.address = new Address(state,city,street);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.name + ":" + this.age + ":" + this.address;  //重写toString方法
    }
}

 

//对象的序列化--将对象以二进制的形式输出(没有确定输出到哪儿去)
        ArrayList<Student> al = new ArrayList<Student>();
        Student stu0 = new Student("HJ", 25,"四川","成都","九眼桥第三桥洞");
        Student stu1 = new Student("YP", 27,"四川","成都","九眼桥第2桥洞");
        al.add(stu0);
        al.add(stu1);
        
        ObjectOutputStream oos = null;
        
        try {
            oos = new ObjectOutputStream(new FileOutputStream("student.data"));  //两根通道的对接
            oos.writeObject(al);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(oos != null){
                try {
                    oos.close();   //用完一定记得关闭通道
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
        //反序列化--将输入的二进制流转换为对象(同样不确定二进制流的数据源)
        ArrayList<Student> al = null;
        ObjectInputStream ois = null;
        
        try {
            //使用了装饰器模式
            ois = new ObjectInputStream(new FileInputStream("student.data"));        
            al = (ArrayList<Student>)ois.readObject();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        System.out.println(al.get(0));
        System.out.println(al.get(1));   //打印看效果
    }

posted @ 2016-06-26 22:42  Blueses  阅读(97)  评论(0编辑  收藏  举报