java序列化和反序列化
概念:
java序列化是指将类对象转化成二进制流,序列化的目的是为了方便信息的传输和存储
java反序列化是指将二进制流转化成类对象。
下面代码实现了序列化和反序列化:
1、首先创建Student.java,它实现了Serializable接口,并给serialVersionUID赋值为1L。
package htRQI; import java.io.Serializable; public class Student implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; private int age; 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; } }
2、测试
package htRQI; 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; public class TestSerialize { public static void main(String[] args) { Student s=new Student(); s.setName("Bonnie"); s.setAge(20); try { serialize(s); unserialize(); }catch(Exception e){ e.printStackTrace(); } } /** * 序列化 * @param s * @throws FileNotFoundException * @throws IOException */ public static void serialize(Student s) throws FileNotFoundException, IOException { File file=new File("D:/a.txt"); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file)); out.writeObject(s); System.out.println("序列化完成"); out.close(); } /** * 反序列化 * @throws FileNotFoundException * @throws IOException * @throws ClassNotFoundException */ public static void unserialize() throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("D:/a.txt"))); Student s=(Student) in.readObject(); System.out.println("反序列化完成"); System.out.println("name:"+s.getName()+" age:"+s.getAge()); in.close(); } }
输出结果:
序列化完成
反序列化完成
name:Bonnie age:20
知识点:(1)Serializable接口中没有定义任何方法,它的作用是标识实现它的类可以被序列化。
(2)实现Serializable接口后需要给serialVersionUID赋值。serialVersionUID的作用是用于判断序列化前的对象和序列化得到的对象是否一致。可以做个测试:
serialVersionUID的值改为2L
package htRQI; import java.io.Serializable; public class Student implements Serializable{ /** * */ private static final long serialVersionUID = 2L; private String name; private int age; 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; } }
将serialize(s);注释掉
package htRQI; 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; public class TestSerialize { public static void main(String[] args) { Student s=new Student(); s.setName("Bonnie"); s.setAge(20); try { //serialize(s); unserialize(); }catch(Exception e){ e.printStackTrace(); } } /** * 序列化 * @param s * @throws FileNotFoundException * @throws IOException */ public static void serialize(Student s) throws FileNotFoundException, IOException { File file=new File("D:/a.txt"); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file)); out.writeObject(s); System.out.println("序列化完成"); out.close(); } /** * 反序列化 * @throws FileNotFoundException * @throws IOException * @throws ClassNotFoundException */ public static void unserialize() throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("D:/a.txt"))); Student s=(Student) in.readObject(); System.out.println("反序列化完成"); System.out.println("name:"+s.getName()+" age:"+s.getAge()); in.close(); } }
会报错:
java.io.InvalidClassException: htRQI.Student; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:687) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1876) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1745) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2033) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427) at htRQI.TestSerialize.unserialize(TestSerialize.java:45) at htRQI.TestSerialize.main(TestSerialize.java:19)
这是因为序列化的时候serialVersionUID的值为1L,而反序列化的时候值为2L。不一致导致invalidClassException。所以这个serialVersionUID需要显示为其赋值,并设为final