基于字符流,實現Manger對象的持久化,并測試。
io的問題 基于字符流,實現Manger對象的持久化,并測試。
qq_擺攤賣憂傷_0
2017-01-05 17:35:09
TA貢獻3條經驗 獲得超1個贊
// ?對象序列化后,保存到文件中去
String file = "demo/obj.dat";
//實例化一個ObjectOutputStream,參數是一個文件字節流FileOutputStream
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
//實例化一個對象
Student stu = new Student("10001", "小明", 20);
//寫入文件
oos.writeObject(stu);
oos.flush();
oos.close();
//反序列化
String file = "demo/obj.dat";
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(file));
Student stu = (Student)ois.readObject();
System.out.println(stu);
ois.close();
舉報