Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象。 序列化是Java中实现持久化存储的一种方法;为数据传输提供了线路级对象表示法。
Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常
方法:1.运行程序 序列化对象保存本地文件SerializeCustomer();// 序列化Customer对象
//Customer customer = DeserializeCustomer();// 反序列Customer对象
//System.out.println(customer);
2.类Customer 添加成员变量 反序列化应用 修改如下
// SerializeCustomer();// 序列化Customer对象
Customer customer = DeserializeCustomer();// 反序列Customer对象
System.out.println(customer);
private String tt;
运行报错:
hread "main" java.io.InvalidClassException: Customer; local class incompatible: stream classdesc serialVersionUID = -88175599799432325, local class serialVersionUID = 7060251297219557883
修改上上述问题:添加private static final long serialVersionUID = 33333L;
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.io.Serializable;
public class TestSerialversionUID {
public static void main(String[] args) throws Exception {
//SerializeCustomer();// 序列化Customer对象
Customer customer = DeserializeCustomer();// 反序列Customer对象
System.out.println(customer);
}
/**
* MethodName: SerializeCustomer
* Description: 序列化Customer对象
* @author xudp
* @throws FileNotFoundException
* @throws IOException
*/
private static void SerializeCustomer() throws FileNotFoundException,
IOException {
Customer customer = new Customer("gacl",25);
// ObjectOutputStream 对象输出流
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
new File("/Users/ustcyb/Desktop/Umeng/Umeng_Workspace/Umeng-Dev/Umeng-MockServer/TestSerialversionUID/Customer.txt")));
oo.writeObject(customer);
System.out.println("Customer对象序列化成功!");
oo.close();
}
/**
* MethodName: DeserializeCustomer
* Description: 反序列Customer对象
* @author xudp
* @return
* @throws Exception
* @throws IOException
*/
private static Customer DeserializeCustomer() throws Exception, IOException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
new File("/Users/ustcyb/Desktop/Umeng/Umeng_Workspace/Umeng-Dev/Umeng-MockServer/TestSerialversionUID/Customer.txt")));
Customer customer = (Customer) ois.readObject();
System.out.println("Customer对象反序列化成功!");
return customer;
}
}
/**
* <p>ClassName: Customer<p>
* <p>Description: Customer实现了Serializable接口,可以被序列化<p>
* @author xudp
* @version 1.0 V
* @createTime 2014-6-9 下午04:20:17
*/
class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 33333L;
//Customer类中没有定义serialVersionUID
private String name;
private int age;
private String tt;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
/*
* @MethodName toString
* @Description 重写Object类的toString()方法
* @author xudp
* @return string
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "name=" + name + ", age=" + age;
}
}