qq_擺攤賣憂傷_0
2017-01-05 17:35:09
基于字符流,實現(xiàn)Manger對象的持久化,并測試。
1 回答

hlc157248yx
TA貢獻3條經(jīng)驗 獲得超1個贊
// ?對象序列化后,保存到文件中去
String file = "demo/obj.dat";
//實例化一個ObjectOutputStream,參數(shù)是一個文件字節(jié)流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();
添加回答
舉報
0/150
提交
取消