本文主要关注的是二进制序列化后的二进制内容解读。通过解读这些看似枯燥的内容,可以让我们做到心中有底——为什么一端序列化出来的二进制流能在另外一端完整地复原?
1. 样例代码
代码如下, 省略不关心的部分
public class Employee implements Serializable {
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, int year, int month, int day) {
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
}
// 略
}
public class Tester{
private static final String SAVED_PATH = "src/main/java/objectStream/employee.dat";
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 持久化到本地存储中
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(SAVED_PATH))) {
out.writeObject(obj);
}
}
2. 解读
以下是在notepad++下以hex模式打开的文件截图
https://blog.csdn.net/lqzkcx3/article/details/79463450