Java 对象序列化
序列化 将对象转为字节文件
File file = new File("E:\\books\\se.txt");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
//创建对象 参数是输出流对象
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p = new Person("lhh", 14);
//writeObject()方法将对象转为字节输出到文件
oos.writeObject(p);
oos.close();
fos.close();
反序列化 将字节转为对象
FileInputStream fis = new FileInputStream(file);
//创建对象 参数是输入流对象
ObjectInputStream ois = new ObjectInputStream(fis);
//readObject()方法将字节转为对象
Person p1 = (Person) ois.readObject();
System.out.println(p1);
ois.close();
fis.close();