序列化的作用是将数据信息保存到硬盘中;
参与序列化的对象必须实现Serializable接口
- 通过源代码发现Serializable接口只是一个标志接口。
- 接口当里面什么代码都没有。起到标识、标志作用
- 使用transient 关键字修饰时,将不参加序列化。
- 一次序列化多个对象可以将对象放到集合中,序列化集合
创建user类提供属性
单个序列化
public static void main(String[] args) throws IOException {
User s =new User(1111,"lisi");
//序列化
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("user"));
oos.writeObject(s);
oos.flush();
oos.close();
}
序列化集合
public static void main(String[] args) throws IOException {
List<User01> user01List =new ArrayList<>();
user01List.add(new User01(1,'w'));
user01List.add(new User01(2,'a'));
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("user01"));
oos.writeObject(user01List);
oos.flush();
oos.close();
反序列化集合
}
public static void main(String[] args) throws IOException {
User s =new User(1111,“lisi”);
//序列化
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream(“user”));
oos.writeObject(s);
oos.flush();
oos.close();
}