1.對象的序列化就是儲存對象的目前狀態。
2.儲存對象的狀態有兩種主要的方式:
1) 對象序列化(將對象寫入到檔案,以後再還原序列化,見後例)
如果儲存的資料用於Java程式內互動,則採用對象的序列化;
2) 寫入檔案(將對象寫入到文字檔,注意,是文字檔)
如果儲存的資料用於和其他非Java程式互動,則儲存到文字檔。
3.對象如果需要序列化,則它所在的類必須要實現Serializable介面。
如果某類是可序列化的,則它的子類自動地可以序列化。
4.對象序列化的步驟:
1) 建立檔案輸出資料流;
2) 建立對象輸出資料流;
3) 寫入對象狀態;
4) 關閉對象輸出資料流;
//1.建立檔案輸出資料流FileOutputStream fs = new FileOutputStream("OutputFile.file");//2.建立對象輸出資料流ObjectOutputStream os = new ObjectOutputStream(fs);//3.寫入對象狀態os.writeObject(characterOne);os.writeObject(characterTwo);os.writeObject(characterThree);//4.關閉對象輸出資料流os.close();
對象寫入檔案的順序為:
其中,ObjectOutputStream是把對象轉換成位元組流;FileOutputStream是把位元組流寫入檔案。
5.序列化對象會把對象的所有屬性儲存,被對象的執行個體變數所引用的對象也會被序列化。
如果某執行個體變數不能或不應該被序列化,則把這個變數標記為transient。將屬性標記為transient後,對象序列化時會將該屬性儲存為null值。
6.對象解序列化的步驟:
1) 建立檔案輸入資料流;
2) 建立對象輸入資料流;
3) 讀出對象狀態;
4) 轉換物件類型(因為對象讀出後為Object類型);
5) 關閉對象輸入資料流;
//1.建立檔案輸入資料流,如果檔案不存在,則程式拋出異常FileInputStream fs = new FileInputStream("OutputFile.file");//2.建立對象輸入資料流ObjectInputStream oi = new ObjectInputStream(fs);//3.讀出對象狀態Object one = oi.readObject();Object two = oi.readObject();Object three = oi.readObject();//4.轉換物件類型GameCharacter elf = (GameCharacter) one;GameCharacter troll = (GameCharacter) two;GameCharacter magician = (GameCharacter) three;//5.關閉對象輸入資料流oi.close();
對象寫出的順序為:
在對象讀出到對象輸入資料流後,JVM會嘗試尋找和載入對象的類,如果不成功則報異常。
7.對象被還原時,靜態變數維持類中原本的樣子,而不是儲存時的樣子。
8.將對象狀態寫入檔案的步驟:(使用BufferedWriter)
//先用緩衝區存放資料,然後連結到檔案BufferedWriter writer = new BufferedWriter(new FileWriter(aFile));//寫入資料writer.write("blabla");//關閉writer.close();
對象寫入檔案的順序:
使用緩衝區Writer的原因:如果直接使用FileWriter的話,寫硬碟需要好多次,速度較慢(硬碟讀寫速度遠遠慢於記憶體讀寫速度)。使用了緩衝區Writer就可以先緩衝資料,然後一次性得寫到硬碟檔案中。
9.將對象從檔案讀出的步驟:(使用BufferedReader)
//FileReader是字元的串連到文字檔的串流FileReader fileReader = new FileReader(new File("File.txt"));//使用BufferedReader連結到FileReader,使得接收更有效率BufferedReader reader = new BufferedReader(fileReader);String line = null;//挨行列印出資料while((line = reader.readLine())!= null){System.out.println(line);}
檔案讀出的過程中,使用BufferedReader去連結FileReader,使得接收更有效率。
---------------------------------------
以下是一個對象序列化,還原序列化以及檔案IO的例子,以後可以翻閱
class CashCheck implements Serializable{private static final long serialVersionUID = -8872155900725314694L;private String cashCheckID;private int cashAmout;public CashCheck(){}public String getCashCheckID(){return cashCheckID;}public void setCashCheckID(String cashCheckID){this.cashCheckID = cashCheckID;}public int getCashAmout(){return cashAmout;}public void setCashAmout(int cashAmout){this.cashAmout = cashAmout;}}public class SerializeDemo01{public static void main(String[] args){CashCheck check = new CashCheck();check.setCashAmout(10000);check.setCashCheckID("notifier");/* * 序列化對象 */try{// 1.建立檔案輸出資料流,負責將位元組寫入檔案FileOutputStream fs = new FileOutputStream(new File("C:\\Documents and Settings\\Administrator\\案頭\\notifier.file"));// 2.建立對象輸出資料流,負責將對象轉為位元組ObjectOutputStream os = new ObjectOutputStream(fs);// 3.寫入對象os.writeObject(check);// 4.關閉對象輸出資料流os.close();} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}/* * 解序列化對象 */try{// 1.建立檔案輸入資料流,負責將檔案轉為位元組FileInputStream fi = new FileInputStream("C:\\Documents and Settings\\Administrator\\案頭\\notifier.file");// 2.建立對象輸入資料流,負責將位元組寫入對象ObjectInputStream oi = new ObjectInputStream(fi);// 3.讀出對象Object o1 = oi.readObject();if (o1 instanceof CashCheck){CashCheck check1 = (CashCheck) o1;System.out.println(check1.getCashCheckID());System.out.println(check1.getCashAmout());}// 4.關閉對象輸入資料流oi.close();} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();} catch (ClassNotFoundException e){e.printStackTrace();}/* * 以檔案的形式讀出序列化檔案,是亂碼 */try{FileReader fr = new FileReader(new File("C:\\Documents and Settings\\Administrator\\案頭\\notifier.file"));BufferedReader br = new BufferedReader(fr);String line = null;while ((line = br.readLine()) != null){System.out.println(line);}} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}