猿问

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

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


我的应用类:


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;

}


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


慕尼黑8549860
浏览 88回答 1
1回答

猛跑小猪

在从文件加载 Person 之前,您正在保存一个空列表。我建议这种方法:import javax.swing.*;import java.io.*;import java.util.ArrayList;import java.util.List;public class App extends JFrame {&nbsp; &nbsp; public static JComboBox<Person> peopleBox;&nbsp; &nbsp; public App() {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadList();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException | ClassNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saveList(Person.peopleList);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; setSize(600, 400);&nbsp; &nbsp; &nbsp; &nbsp; setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);&nbsp; &nbsp; }&nbsp; &nbsp; public void updateData(){&nbsp; &nbsp; &nbsp; &nbsp; peopleBox = new JComboBox<>();&nbsp; &nbsp; &nbsp; &nbsp; peopleBox.setModel(getComboBoxModel(Person.peopleList));&nbsp; &nbsp; &nbsp; &nbsp; add(peopleBox);&nbsp; &nbsp; &nbsp; &nbsp; pack();&nbsp; &nbsp; }&nbsp; &nbsp; public DefaultComboBoxModel<Person> getComboBoxModel(ArrayList<Person> peopleList) {&nbsp; &nbsp; &nbsp; &nbsp; Person[] comboBoxModel = peopleList.toArray(new Person[0]);&nbsp; &nbsp; &nbsp; &nbsp; return new DefaultComboBoxModel<>(comboBoxModel);&nbsp; &nbsp; }&nbsp; &nbsp; public static void saveList(ArrayList<Person> peopleList) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.bin"));&nbsp; &nbsp; &nbsp; &nbsp; objectOutputStream.writeObject(peopleList);&nbsp; &nbsp; }&nbsp; &nbsp; public static void loadList() throws IOException, ClassNotFoundException {&nbsp; &nbsp; &nbsp; &nbsp; ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.bin"));&nbsp; &nbsp; &nbsp; &nbsp; Person.peopleList.addAll((List<Person>) objectInputStream.readObject());&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; App app = new App();&nbsp; &nbsp; &nbsp; &nbsp; Person p = new Person("Sean2", 24);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saveList(Person.peopleList);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; app.updateData();&nbsp; &nbsp; &nbsp; &nbsp; app.setVisible(true);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答