如何在 Java 11 中关闭打开的程序后反序列化对象

How to deserialize objects after closing an opening program in Java 11

我有一个程序,我试图在其中实现对象的保存和加载,但是我无法在程序关闭后加载工作,所以只有在程序打开时才能有效地保存和加载,但是一旦程序启动,就不会加载任何数据。我认为这与过度使用有关。我创建了一个测试程序,看看我是否可以只使用一个简单的 Person class 来让它工作。我将我的 Peson 对象存储在 ArrayList 中并序列化它,然后反序列化它。目前我将所有加载的 Person 对象存储在 JComboBox 中。我在网上看过,找不到任何有用的东西。另请注意,我知道使用序列化不是保存对象的最佳方法,但它适合我的程序使用。

我的应用程序Class:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

public class App  extends JFrame {
    public static JComboBox<Person> peopleBox;
    public App(){
        try {
            Person.peopleList = loadList();
        }
        catch(IOException | ClassNotFoundException e){
            System.out.println(e.getMessage());
        }
        try {
            saveList(Person.peopleList);
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
        peopleBox = new JComboBox<>();
        peopleBox.setModel(getComboBoxModel(Person.peopleList));
        add(peopleBox);
        pack();
        setSize(600, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public DefaultComboBoxModel<Person> getComboBoxModel(ArrayList<Person> peopleList){
        Person[] comboBoxModel = peopleList.toArray(new Person[0]);
        return new DefaultComboBoxModel<>(comboBoxModel);
    }
    public static void saveList(ArrayList<Person> peopleList) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.bin"));
        objectOutputStream.writeObject(peopleList);
    }
    public static ArrayList<Person> loadList() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.bin"));
        Person.peopleList = (ArrayList<Person>) objectInputStream.readObject();
        return  Person.peopleList;
    }

    public static void main(String[] args){
       // Person p  = new Person("Sean", 22);
        try {
            saveList(Person.peopleList);
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
        App app = new App();
        app.pack();
        app.setVisible(true);
    }

}

个人Class

import java.io.Serializable;
import java.util.ArrayList;

public class Person implements Serializable {

    public int age;
    public String name;
    public static ArrayList<Person> peopleList = new ArrayList<>();

    public Person(String name, int age){
        this.age = age;
        this.name = name;
        peopleList.add(this);
        for(Person p : peopleList){
            System.out.println(p.toString());
        }
    }

    public Person(){
    }

    public String toString(){
        return "Name : " + name + " Age: " + age;
    }
}

我希望当我将列表保存到 "test.bin" 文件时,关闭程序,然后再次打开它,它将加载列表并显示我在关闭程序之前创建的对象。感谢您的帮助,谢谢。

在从文件加载 Person 之前,您正在保存一个空列表。 我建议这种方法:

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class App extends JFrame {

    public static JComboBox<Person> peopleBox;

    public App() {
        try {
            loadList();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        try {
            saveList(Person.peopleList);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        setSize(600, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void updateData(){
        peopleBox = new JComboBox<>();
        peopleBox.setModel(getComboBoxModel(Person.peopleList));
        add(peopleBox);
        pack();
    }

    public DefaultComboBoxModel<Person> getComboBoxModel(ArrayList<Person> peopleList) {
        Person[] comboBoxModel = peopleList.toArray(new Person[0]);
        return new DefaultComboBoxModel<>(comboBoxModel);
    }

    public static void saveList(ArrayList<Person> peopleList) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.bin"));
        objectOutputStream.writeObject(peopleList);
    }

    public static void loadList() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.bin"));
        Person.peopleList.addAll((List<Person>) objectInputStream.readObject());
    }

    public static void main(String[] args) {
        App app = new App();
        Person p = new Person("Sean2", 24);
        try {
            saveList(Person.peopleList);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        app.updateData();
        app.setVisible(true);
    }
}