ObjectOutputStream 類用來序列化一個對象,如下的SerializeDemo例子實例化了一個Employee對象,并將該對象序列化到一個文件中。
該程序執(zhí)行后,就創(chuàng)建了一個名為employee.ser文件。該程序沒有任何輸出,但是你可以通過代碼研讀來理解程序的作用。
注意: 當序列化一個對象到文件時, 按照Java的標準約定是給文件一個.ser擴展名。
import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Reyan Ali"; e.address = "Phokka Kuan, Ambehta Peer"; e.SSN = 11122333; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in /tmp/employee.ser"); }catch(IOException i) { i.printStackTrace(); } } }
2015職稱計算機考試書PowerPoint2007中 .. 定價:¥45 優(yōu)惠價:¥42 更多書籍 | |
2015年全國職稱計算機考試教材(2007模 .. 定價:¥225 優(yōu)惠價:¥213 更多書籍 |