序列化 Serializable
1.Serializable
类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化,并抛出异常;
异常代码:
1 java.io.NotSerializableException: cn.a2.serializable.Student 2 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180) 3 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 4 at cn.a2.serializable.Test1.main(Test1.java:22)
1 package cn.a2.serializable; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable { 6 7 private String id; 8 private String name; 9 private int age; 10 private Gender gender; 11 public String getId() { 12 return id; 13 } 14 public void setId(String id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 public Gender getGender() { 30 return gender; 31 } 32 public void setGender(Gender gender) { 33 this.gender = gender; 34 } 35 public Student(String id, String name, int age, Gender gender) { 36 super(); 37 this.id = id; 38 this.name = name; 39 this.age = age; 40 this.gender = gender; 41 } 42 public Student() { 43 super(); 44 } 45 46 @Override 47 public String toString() { 48 return "Student [id=" + id + ", name=" + name + ", age=" + age 49 + ", gender=" + gender + "]"; 50 } 51 52 53 } 54 55 enum Gender { 56 男, 女; 57 58 }
把属性信息写入d:"+File.separator+"javatest"+File.separator+"e.txt文件里:
1 package cn.a2.serializable; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.ObjectOutputStream; 8 9 public class Test1 { 10 public static void main(String[] args) throws FileNotFoundException, IOException { 11 Student student = new Student("1001", "小明", 20, Gender.男); 12 13 /* 14 *取stu所有属性,通过特定的字符串(-),把各个属性值连接起来 15 * 1001-小明-20-男 16 */ 17 18 File file = new File("d:"+File.separator+"javatest"+File.separator+"e.txt"); 19 FileOutputStream fos = new FileOutputStream(file); 20 ObjectOutputStream oos = new ObjectOutputStream(fos); 21 22 oos.writeObject(student); 23 24 oos.close(); 25 fos.close(); 26 27 28 } 29 30 }
反序列化
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.ObjectInputStream; 6 7 public class Test2 { 8 public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 9 File file = new File("d:"+File.separator+"javatest"+File.separator+"e.txt"); 10 11 FileInputStream iStream = new FileInputStream(file); 12 ObjectInputStream ois = new ObjectInputStream(iStream); 13 14 Student student = (Student)ois.readObject(); 15 System.out.println(student.getId()); 16 System.out.println(student.getName()); 17 System.out.println(student.getAge()); 18 System.out.println(student.getGender()); 19 20 iStream.close(); 21 ois.close(); 22 } 23 24 }
此时可能会出现异常
注意:
当序列化完成后,后期推送升级程序中的类(Student), 此时再反序列化时会出现异常。
1 Exception in thread "main" java.io.InvalidClassException: cn.sxt05.serializable.Student; local class incompatible: stream classdesc serialVersionUID = -6288733824962181189, local class serialVersionUID = 1690603786167234505 2 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:687) 3 at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1876) 4 at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1745) 5 at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2033) 6 at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567) 7 at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
主要原因是没有给Student类加序列化版本号, 添加了就可以解决企业版本推送更新发生版本异常问题;
添加方式有两种:
以下代码添加后版本号之后,就可以解决推送更新版本号的问题;
1 package cn.a2.serializable; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable { 6 /** 7 * 8 */ 9 private static final long serialVersionUID = -4427440848479890883L;//给Student类加序列化版本号,可以解决企业版本推送更新发生版本异常问题。 10 11 private String id; 12 private String name; 13 private int age; 14 private Gender gender; 15 public String getId() { 16 return id; 17 } 18 public void setId(String id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public int getAge() { 28 return age; 29 } 30 public void setAge(int age) { 31 this.age = age; 32 } 33 public Gender getGender() { 34 return gender; 35 } 36 public void setGender(Gender gender) { 37 this.gender = gender; 38 } 39 public Student(String id, String name, int age, Gender gender) { 40 super(); 41 this.id = id; 42 this.name = name; 43 this.age = age; 44 this.gender = gender; 45 } 46 public Student() { 47 super(); 48 } 49 50 @Override 51 public String toString() { 52 return "Student [id=" + id + ", name=" + name + ", age=" + age 53 + ", gender=" + gender + "]"; 54 } 55 56 57 } 58 59 enum Gender { 60 男, 女; 61 62 }
反序列化
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.ObjectInputStream; 6 7 public class Test2 { 8 public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 9 File file = new File("d:"+File.separator+"javatest"+File.separator+"e.txt"); 10 11 FileInputStream iStream = new FileInputStream(file); 12 ObjectInputStream ois = new ObjectInputStream(iStream); 13 14 Student student = (Student)ois.readObject(); 15 System.out.println(student.getId()); 16 System.out.println(student.getName()); 17 System.out.println(student.getAge()); 18 System.out.println(student.getGender()); 19 20 iStream.close(); 21 ois.close(); 22 } 23 24 }
添加后版本号之后,就可以解决了更新版本号异常问题,所以以后企业开发过程中,一定要记得添加版本号。