12 自定义一个对象使用ObjectInputstream和ObjectOutputStream存储到硬盘上并读取
public class Student implements Serializable {}
Student str
= new Student("张三",35);
ObjectOutputStream object
=null
;
try {
object
= new ObjectOutputStream(new FileOutputStream("e:\\测试类文件\\Student.txt"));
object
.writeObject(str
);
object
.flush();
} catch (Exception e
) {
e
.printStackTrace();
}finally{
try {
object
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
ObjectInputStream In
=null
;
try{
In
= new ObjectInputStream(new FileInputStream("e:\\测试类文件\\Student.txt"));
Student St
=(Student
)In
.readObject();
System
.out
.println(St
);
}catch (Exception e
) {
e
.printStackTrace();
}finally{
try {
In
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
转载请注明原文地址: https://mac.8miu.com/read-501885.html