gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:47:21 24 4
gpt4 key购买 nike

我有一个程序,我试图在其中实现对象的保存和加载,但是在程序关闭后我无法让加载工作,因此实际上只有在程序打开时才保存和加载工作,但没有一旦程序启动,数据就会被加载。我认为这与过度智慧有关。我创建了一个测试程序,看看是否可以仅使用一个简单的 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;
}

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);
}

}

人员类别

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”文件时,关闭程序,然后再次打开它,它将加载列表并显示我在关闭程序之前创建的对象。我很感激任何帮助,谢谢。

最佳答案

在从文件加载人员之前,您将保存一个空列表。我建议采用这种方法:

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);
}
}

关于java - 如何在 Java 11 中关闭打开的程序后反序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com