代码示例
package com.haimeng.mybatis.pojo;
import java.io.*;
public class UserIO {
public static void main(String[] args) throws Exception {
System.out.println("user对象的反序列化");
User user = new User(1, "DMT", "123456", 1, "女", "123456@qq.com");
FileOutputStream fos = new FileOutputStream("D:\\代码\\Mybatis\\MyBatis_demo1\\src\\main\\java\\com\\haimeng\\mybatis\\pojo\\a.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(user);
fos.close();
oos.close();
FileInputStream fis = new FileInputStream("D:\\代码\\Mybatis\\MyBatis_demo1\\src\\main\\java\\com\\haimeng\\mybatis\\pojo\\a.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
System.out.println(o.toString());
fis.close();
ois.close();
}
}
注意事项
- 如果要序列化或者反序列化必须实现Serializable接口
- 如果某个属性不想被序列化可以使用transient修饰