1.1 概念
对象序列化机制(Object Serialzation)是Java语言内建的一种轻量级持久化方式,可以容易的在JVM的活动对象信息与字节序列之间转化(序列化与反序列化),用来屏蔽底层实现细节。
1.2 意义
2.1 基本对象序列化
1 public class StudySerializable{ 2 public static void main(String[] args) 3 throws IOException, ClassNotFoundException{ 4 Car car = new Car("Volvo"); 5 6 System.out.println("serialize"); 7 System.out.println(car); 8 ObjectOutputStream out = new ObjectOutputStream( 9 new FileOutputStream("E://java//car.dat")); 10 out.writeObject(car); 11 12 System.out.println("deserialize"); 13 ObjectInputStream in = new ObjectInputStream( 14 new FileInputStream("E://java//car.dat")); 15 Car car1 = (Car) in.readObject(); 16 System.out.println(car1); 17 } 18 } 19 class Car implements Serializable { 20 private static final long serialVersionUID = 8217070173670578351L; 21 private String name; 22 23 Car(String name){ 24 this.name = name; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 @Override 32 public String toString() { 33 return "Car name is " + name; 34 } 35 }output: 36 serialize 37 Car name is Volvo 38 deserialize 39 Car name is Volvo
2.2 静态变量序列化以及继承、依赖序列化
1 public class StudySerializable{ 2 public static void main(String[] args) 3 throws IOException, ClassNotFoundException{ 4 Car car = new Car(); 5 6 System.out.println("serialize"); 7 System.out.println(Car.staticNum); 8 ObjectOutputStream out = new ObjectOutputStream( 9 new FileOutputStream("E://java//car.dat")); 10 out.writeObject(car); 11 12 Car.staticNum = 5; 13 14 System.out.println("deserialize"); 15 ObjectInputStream in = new ObjectInputStream( 16 new FileInputStream("E://java//car.dat")); 17 Car car1 = (Car) in.readObject(); 18 System.out.println(Car.staticNum); 19 } 20 } 21 class Car implements Serializable{ 22 private static final long serialVersionUID = 8217070173670578351L; 23 public static int staticNum = 10; 24 }output: 25 serialize 26 10 27 deserialize 28 5
2.3 transient关键字与序列化ID
1 public class StudySerializable{ 2 public static void main(String[] args) 3 throws IOException, ClassNotFoundException{ 4 User user = new User("kanyuxia@outlook.com ", "123456"); 5 6 System.out.println("serialize"); 7 System.out.println(user); 8 ObjectOutputStream out = new ObjectOutputStream( 9 new FileOutputStream("E:\\java\\user.dat")); 10 out.writeObject(user); 11 12 System.out.println("deserialize"); 13 ObjectInputStream in = new ObjectInputStream( 14 new FileInputStream("E:\\java\\user.dat")); 15 User user1 = (User) in.readObject(); 16 System.out.println(user1); 17 } 18 } 19 class User implements Serializable { 20 private static final long serialVersionUID = 4936874859415237692L; 21 private String userName; 22 private transient String password; 23 24 User(String userName, String password){ 25 this.userName = userName; 26 this.password = password; 27 } 28 29 @Override 30 public String toString() { 31 return "userName: " + userName + " password: " + password; 32 } 33 }
2.4 自定义序列化
1 public class StudySerializable{ 2 public static void main(String[] args) 3 throws IOException, ClassNotFoundException{ 4 User user = new User("kanyuxia@outlook.com ", "123456"); 5 6 System.out.println("serialize"); 7 System.out.println(user); 8 ObjectOutputStream out = new ObjectOutputStream( 9 new FileOutputStream("E:\\java\\user.dat")); 10 out.writeObject(user); 11 12 System.out.println("deserialize"); 13 ObjectInputStream in = new ObjectInputStream( 14 new FileInputStream("E:\\java\\user.dat")); 15 User user1 = (User) in.readObject(); 16 System.out.println(user1); 17 } 18 } 19 class User implements Serializable { 20 private static final long serialVersionUID = 4936874859415237692L; 21 private String userName; 22 private transient String password; 23 24 private void writeObject(ObjectOutputStream outputStream) 25 throws IOException, ClassNotFoundException { 26 outputStream.defaultWriteObject(); 27 outputStream.writeObject(password); 28 } 29 30 private void readObject(ObjectInputStream inputStream) 31 throws IOException, ClassNotFoundException { 32 inputStream.defaultReadObject(); 33 password = (String) inputStream.readObject(); 34 } 35 36 User(String userName, String password){ 37 this.userName = userName; 38 this.password = password; 39 } 40 41 @Override 42 public String toString() { 43 return "userName: " + userName + " password: " + password; 44 } 45 }output: 46 serialize 47 userName: kanyuxia@outlook.com password: 123456 48 deserialize 49 userName: kanyuxia@outlook.com password: 123456
2.5 序列化安全性以及RMI
2.6 序列化与反序列化
http://www.infoq.com/cn/articles/cf-java-object-serialization-rmi
https://www.ibm.com/developerworks/cn/java/j-lo-serial/
原文:http://www.cnblogs.com/maying3010/p/6510798.html