一:需求分析
在实际的B/S结构中,对象的序列化和反序列化很重要,尤其是请求量比较大的情况,服务器压力很大,
会把一部分session序列化,然后保存到硬盘中。
二:定义DTO对象
定义的实体类一定要实现 Serializable接口,才可以序列化。
1 /** 2 * 3 */ 4 package com.hlcui.dto; 5 6 import java.io.Serializable; 7 8 /** 9 * @author Administrator 学生实体类 10 */ 11 public class Student implements Serializable{ 12 13 /** 14 * 15 */ 16 private static final long serialVersionUID = 1L; 17 private Integer id; 18 private String name; 19 private Integer age; 20 private Double score; 21 22 public Student() { 23 24 } 25 26 public Student(Integer id, String name, Integer age, Double score) { 27 super(); 28 this.id = id; 29 this.name = name; 30 this.age = age; 31 this.score = score; 32 } 33 34 public Integer getId() { 35 return id; 36 } 37 38 public void setId(Integer id) { 39 this.id = id; 40 } 41 42 public String getName() { 43 return name; 44 } 45 46 public void setName(String name) { 47 this.name = name; 48 } 49 50 public Integer getAge() { 51 return age; 52 } 53 54 public void setAge(Integer age) { 55 this.age = age; 56 } 57 58 public Double getScore() { 59 return score; 60 } 61 62 public void setScore(Double score) { 63 this.score = score; 64 } 65 66 @Override 67 public String toString() { 68 return "Student [age=" + age + ", id=" + id + ", name=" + name 69 + ", score=" + score + "]"; 70 } 71 72 }
三:封装序列化与反序列化的方法
1 // 序列化对象 2 public static void serlizableObject(Student stu) throws IOException { 3 FileOutputStream fos = new FileOutputStream(new File("F:/stu.txt")); 4 ObjectOutputStream ois = new ObjectOutputStream(fos); 5 ois.writeObject(stu); 6 ois.flush(); 7 ois.close(); 8 } 9 10 // 反序列化对象 11 public static Student tranferSerObject() throws Exception { 12 FileInputStream fis = new FileInputStream(new File("F:/stu.txt")); 13 @SuppressWarnings("resource") 14 ObjectInputStream ois = new ObjectInputStream(fis); 15 return (Student) ois.readObject(); 16 }
四:测试
1 // 测试 2 public static void main(String[] args) throws Exception { 3 Student stu = new Student(); 4 stu.setId(1); 5 stu.setName("Tom"); 6 stu.setAge(26); 7 stu.setScore(9000.0); 8 serlizableObject(stu); // 序列化 9 Student student = tranferSerObject(); 10 System.out.println(student); 11 }
五:显示结果
六:注意点
在实体类Student类里面有个序列版本号,如果不写,那么在改变实体中的某些属性时,可能没法反序列化。
1:去掉版本号,然后执行序列化。
2:在Student类中在添加一个属性,性别gender
3:执行反序列化,会报如下错误。
Exception in thread "main" java.io.InvalidClassException: com.hlcui.dto.Student; local class incompatible: stream classdesc serialVersionUID = 8188927919700027025, local class serialVersionUID = -5275569862405728323
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:562)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.hlcui.io.ObjectSer.tranferSerObject(ObjectSer.java:35)
at com.hlcui.io.ObjectSer.main(ObjectSer.java:46)
没法进行反序列化。
4:如果我们加上版本号,在序列化之后,再添加属性,进行反序列化后
以上代码均已经验证!
原文:http://www.cnblogs.com/warrior4236/p/5686555.html