对象流
ObjectInputStream:对象输入流
ObjectOutputStream:对象输出流
对对象进行存储和读取,给定一个类Student
对象存储/序列化:
/对象存储:序列化
public static void testObjectOutputStream(String path) {
try{
//打开对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
//创建对象
Student student = new Student();
student.setId(1);
student.setName("小红");
student.setAge(18);
//通过writeObject写入对象
oos.writeObject(student);
//关闭流
oos.close();
}catch (IOException e){
e.printStackTrace();
}
}
注意:
1、对象的存储需要使用ObjectOutputStream对象流,并调用其特有的方法WriterObject
2、要能进行正常的对象存储,对象本身需要实现Serializable接口,否则会抛出异常NotSerializableException
对象获取/反序列化:
//对象读取
public static void testObjectInputStream(String path) {