🌺🌺① 对象流
🌺🌺② 序列化
🌺🌺③ 使用对象流和序列化保存和读取图形对象
🌺🌺④ 使用对象流和序列化保存和读取集合对象
以下是一个经典代码示例,演示了如何使用对象流将一个Person对象保存到文件中并读取回来。
import java.io.*;
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 将Person对象保存到文件中
Person person = new Person("Tom", 20);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.obj"));
out.writeObject(person);
out.close();
// 从文件中读取Person对象