1. import java.io.*;
    2. public class Main {
    3. public static void main(String[] args) throws Exception {
    4. String path = "./data.txt";
    5. boolean append = false;
    6. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path, append));
    7. oos.writeObject(new Person("张三", 18));
    8. oos.writeInt(100);
    9. oos.writeUTF("quwgeuiy");
    10. oos.close();
    11. System.out.println("已经写入本地文件");
    12. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
    13. System.out.println(ois.readObject());
    14. System.out.println(ois.readInt());
    15. System.out.println(ois.readUTF());
    16. ois.close();
    17. }
    18. }
    19. class Person implements Serializable {
    20. private String name;
    21. private int age;
    22. public Person(String name, int age) {
    23. this.name = name;
    24. this.age = age;
    25. }
    26. @Override
    27. public String toString() {
    28. return "A{" +
    29. "age=" + age +
    30. ", name='" + name + '\'' +
    31. '}';
    32. }
    33. }