序列化,反序列化要一致

序列化同反序列化的package还有类名一致, 同时两个类里面的serialVersionUID 也要定义一致。 否则就会失败。。

Person类,注意package名字 (test.serialize.person1)

package test.serialize.person1;

import java.io.Serializable;

public class Person implements Serializable {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -5809782578272943999L;
    private int age;
    private String name;
    private String sex;

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

    public void setSex(String sex) {
        this.sex = sex;
    }
}

Person类, package同上一个不同。( test.serialize.person2)

package test.serialize.person2;

import java.io.Serializable;

public class Person implements Serializable {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -5809782578272943999L;
    private int age;
    private String name;
    private String sex;

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

    public void setSex(String sex) {
        this.sex = sex;
    }
}

测试类,序列化反序列化,如果是相同类名,package也一致,serialVersionUID一致,就会转换成功。。

但是package不同,就失败了。。。

测试类  TestObjSerializeAndDeserialize


package test.serialize;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.MessageFormat;


/**
 * ClassName: TestObjSerializeAndDeserialize
 * Description: 测试对象的序列化和反序列
 */
public class TestObjSerializeAndDeserialize {

    public static void main(String[] args) throws Exception {
        SerializePerson();//序列化Person对象
        test.serialize.person2.Person p = DeserializePerson();//反序列Perons对象
        System.out.println(MessageFormat.format("name={0},age={1},sex={2}",
                                                 p.getName(), p.getAge(), p.getSex()));
    }
    
    /**
     * MethodName: SerializePerson 
     * Description: 序列化Person对象
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void SerializePerson() throws FileNotFoundException,
            IOException {
    	test.serialize.person1.Person person = new test.serialize.person1.Person();
        person.setName("gacl");
        person.setAge(25);
        person.setSex("男");
        // ObjectOutputStream 对象输出流,将Person对象存储到E盘的Person.txt文件中,完成对Person对象的序列化操作
        ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
                new File("C:/Person.txt")));
        oo.writeObject(person);
        System.out.println("Person对象序列化成功!");
        oo.close();
    }

    /**
     * MethodName: DeserializePerson 
     * Description: 反序列Perons对象
     * @return
     * @throws Exception
     * @throws IOException
     */
    private static test.serialize.person2.Person DeserializePerson() throws Exception, IOException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                new File("C:/Person.txt")));
        test.serialize.person2.Person person = (test.serialize.person2.Person) ois.readObject();
        System.out.println("Person对象反序列化成功!");
        return person;
    }

}
运行结果是:

Person对象序列化成功!
Exception in thread "main" java.lang.ClassCastException: test.serialize.person1.Person cannot be cast to test.serialize.person2.Person
at test.serialize.TestObjSerializeAndDeserialize.DeserializePerson(TestObjSerializeAndDeserialize.java:61)
at test.serialize.TestObjSerializeAndDeserialize.main(TestObjSerializeAndDeserialize.java:24)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值