第八章 流 09_ObjectIO
鱼欲遇雨:每日都学习一点,持之以恒,天道酬勤!不能用电脑时,提前补上!(2012.9.2)
Object 流(序列化)
直接将Object写入或读出
TestObjectIO.java代码示例
// TestObjectIO.javaimport java.io.*;public class TestObjectIO {public static void main(String args[]) {T t = new T();t.k = 8;try{FileOutputStream fos = new FileOutputStream("c:/java/IO/testobjectio.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(t);oos.flush();oos.close();FileInputStream fis = new FileInputStream("c:/java/IO/testobjectio.txt");ObjectInputStream ois = new ObjectInputStream(fis);T t1 = (T)ois.readObject();System.out.println(t1.i + " " + t1.j + " " + t1.k + " " + t1.d);ois.close();}catch(ClassNotFoundException e) {e.printStackTrace();}catch(IOException e) {e.printStackTrace();}}}class T implements Serializable {int i = 10;int j = 9;int k = 15;transient double d = 2.3; //透明的,默认值0.0}