1.概念
Java的“對象序列化”能將一個實現了Serializable接口的對象轉換成一組byte,這樣日后要用這個對象的時候,能把這些byte數據恢復出來,并據此重新構建那個對象。
對象序列化能實現“輕量級persistence(lightweight persistence)”。所謂persistence,是指對象的生命周期不是由程序是否運行決定的,在程序的兩次調用之間對象仍然還活著。通過“將做過序列化處理的對象寫入磁盤,等到程序再次運行的時候再把它讀出來”,可以達到persistence的效果。
之所以要在語言里加入對象序列化,是因為要用它來實現兩個重要的功能:
1.Java的遠程方法調用能像調用自己機器上的對象那樣去調用其它機器上的對象。
2.對JavaBean來說,Bean的狀態信息通常是在設計時配置的,這些狀態信息必須保存起來,供程序啟動的時候用,對象序列化就負責這個工作。
java什么時候需要序列化、 對象序列化不僅能保存對象的副本,而且還會跟著對象中的reference把它所引用的對象也保存起來,然后再繼續跟蹤那些對象的reference。
?
2.實現序列化
(1)序列化一個對象只要讓它實現Serializable接口就行了(這是一個“標記接口”,tagging interface,沒有任何方法)。
(2)要想序列化對象,必須先創建一個OutputStream,然后把它嵌進ObjectOutputStream。這是就能用writeObject()方法把對象寫入OutputStream。
(3)讀的時候需要把InputStream嵌到ObjectInputStream中,然后再調用readObject()方法。
?
3.樣例
1 package Entity; 2 3 import java.io.Serializable; 4 5 public class Employee implements Serializable{ 6 7 /* 8 * serialVersionUID作用: 9 * 序列化時為了保持版本的兼容性,即在版本升級時反序列化仍保持對象的唯一性。 10 * 如果你的類Serialized存到硬盤上面后,可是后來你卻更改了類別的field(增加或減少或改名), 11 * 當你Deserialize時,就會出現Exception的,這樣就會造成不兼容性的問題。 12 * 但當serialVersionUID相同時,它就會將不一樣的field以type的預設值Deserialize,這個可以避開不兼容性的問題。 13 */ 14 private static final long serialVersionUID = -5167690587890199758L; 15 16 public String name; 17 public String address; 18 public transient int SSN; 19 public int number; 20 public Department department; 21 22 public String toString() { 23 return "Employee [name=" + name + ", address=" + address + ", number=" + number + "]"; 24 } 25 26 }
1 package Entity; 2 3 import java.io.Serializable; 4 5 public class Department implements Serializable{ 6 private static final long serialVersionUID = -6913084898834515327L; 7 8 public int id; 9 public String name; 10 11 public String toString() { 12 return "Department [id=" + id + ", name=" + name + "]"; 13 } 14 15 }
1 package Test; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.ObjectInputStream; 8 import java.io.ObjectOutputStream; 9 10 import Entity.Department; 11 import Entity.Employee; 12 13 public class test1 { 14 15 /** 16 * 序列化對象 17 */ 18 @org.junit.Test 19 public void SerializeDemo() { 20 Employee e = new Employee(); 21 e.name = "Reyan Ali"; 22 e.address = "Phokka Kuan, Ambehta Peer"; 23 e.SSN = 11122333; 24 e.number = 101; 25 Department d = new Department(); 26 d.id = 1; 27 d.name = "Java研發"; 28 e.department = d; 29 try { 30 System.out.println(e); 31 FileOutputStream fileOut = new FileOutputStream(new File("/Users/zhengbinMac/employee.txt")); 32 ObjectOutputStream out = new ObjectOutputStream(fileOut); 33 System.out.println(e); 34 out.writeObject(e); 35 out.close(); 36 fileOut.close(); 37 System.out.printf("Serialized data is saved in /Users/zhengbinMac/employee.txt"); 38 } catch (IOException i) { 39 i.printStackTrace(); 40 } 41 } 42 43 /** 44 * 反序列化對象 45 */ 46 @org.junit.Test 47 public void DeserializeDemo() { 48 Employee e = null; 49 try { 50 FileInputStream fileIn = new FileInputStream("/Users/zhengbinMac/employee.txt"); 51 ObjectInputStream in = new ObjectInputStream(fileIn); 52 e = (Employee) in.readObject(); 53 in.close(); 54 fileIn.close(); 55 } catch (IOException i) { 56 i.printStackTrace(); 57 return; 58 } catch (ClassNotFoundException c) { 59 System.out.println("Employee class not found"); 60 c.printStackTrace(); 61 return; 62 } 63 System.out.println("Deserialized Employee..."); 64 System.out.println("Name: " + e.name); 65 System.out.println("Address: " + e.address); 66 System.out.println("SSN: " + e.SSN); 67 System.out.println("Number: " + e.number); 68 System.out.println("Department: " + e.department); 69 } 70 }
Java多線程,相關博文:
http://www.cnblogs.com/lanxuezaipiao/p/3369962.html?