import java.io.*; public class TestObjectIO { public static void main(String args[]) throws Exception { Ser se = new Ser(); se.k = 8; FileOutputStream fos = new FileOutputStream("D:\\bak\\log.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(se); oos.flush(); oos.close(); FileInputStream fis = new FileInputStream("D:\\bak\\log.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Ser sReaded = (Ser) ois.readObject(); System.out.println(sReaded.i + " " + sReaded.j + " " + sReaded.d + " " + sReaded.k); } } class Ser implements Serializable {// 把類對象序列化成字節流,把對象寫入網絡或者硬盤必須實現這個接口; int i = 10; int j = 9; double d = 2.3; // int k=18; transient int k = 18;// transient 透明的,序列化時不予考慮; }
結果:10 9 2.3 0
備註:學習用,源自sxt.msbjava