Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java11中关闭打开的程序后反序列化对象_Java_Object_Serialization_Loading - Fatal编程技术网

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

如何在Java11中关闭打开的程序后反序列化对象,java,object,serialization,loading,Java,Object,Serialization,Loading,我有一个程序,我试图在其中实现obejcts的保存和加载,但是我无法在程序关闭后让加载工作,因此有效地只有在程序打开时保存和加载工作,但一旦程序启动,就永远不会加载数据。我想这与过度劳累有关。我创建了一个测试程序,看看我是否可以使用一个简单的Person类使它工作。我将Peson对象存储在ArrayList中并序列化,然后反序列化。目前,我将所有加载的Person对象存储在JComboBox中。我在网上查过,找不到任何有用的东西。还请注意,我知道使用序列化并不是保存对象的最佳方法,但它适合用于我

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

我的应用程序类:

import javax.swing.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.*;
导入java.util.ArrayList;
公共类应用程序扩展了JFrame{
公共静态JComboBox-peopleBox;
公共应用程序(){
试一试{
Person.peopleList=loadList();
}
捕获(IOException | ClassNotFoundException e){
System.out.println(e.getMessage());
}
试一试{
存储列表(Person.peopleList);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
peopleBox=新的JComboBox();
setModel(getComboxModel(Person.peopleList));
添加(peopleBox);
包装();
设置大小(600400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public DefaultComboxModel GetComboxModel(ArrayList peopleList){
Person[]comboBoxModel=peopleList.toArray(新的Person[0]);
返回新的DefaultComboxModel(comboBoxModel);
}
公共静态void存储列表(ArrayList peopleList)引发IOException{
ObjectOutputStream ObjectOutputStream=新的ObjectOutputStream(新文件OutputStream(“test.bin”);
objectOutputStream.writeObject(peopleList);
}
公共静态ArrayList loadList()引发IOException,ClassNotFoundException{
ObjectInputStream ObjectInputStream=新ObjectInputStream(新文件输入流(“test.bin”));
Person.peopleList=(ArrayList)objectInputStream.readObject();
返回Person.peopleList;
}
公共静态void main(字符串[]args){
//人员p=新人(“肖恩”,22岁);
试一试{
存储列表(Person.peopleList);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
App App=新App();
app.pack();
app.setVisible(真);
}
}
人类

import java.io.Serializable;
导入java.util.ArrayList;
公共类Person实现可序列化{
公共信息;
公共字符串名称;
public static ArrayList peopleList=new ArrayList();
公众人物(字符串名称,整数年龄){
这个。年龄=年龄;
this.name=名称;
添加(这个);
对于(人员p:人员列表){
System.out.println(p.toString());
}
}
公众人士(){
}
公共字符串toString(){
返回“姓名:”+姓名+“年龄:”+年龄;
}
}

我希望当我将列表保存到“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);
    }
}
import javax.swing.*;
导入java.io.*;
导入java.util.ArrayList;
导入java.util.List;
公共类应用程序扩展了JFrame{
公共静态JComboBox-peopleBox;
公共应用程序(){
试一试{
loadList();
}捕获(IOException | ClassNotFoundException e){
System.out.println(e.getMessage());
}
试一试{
存储列表(Person.peopleList);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
设置大小(600400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
公共void updateData(){
peopleBox=新的JComboBox();
setModel(getComboxModel(Person.peopleList));
添加(peopleBox);
包装();
}
public DefaultComboxModel GetComboxModel(ArrayList peopleList){
Person[]comboBoxModel=peopleList.toArray(新的Person[0]);
返回新的DefaultComboxModel(comboBoxModel);
}
公共静态void存储列表(ArrayList peopleList)引发IOException{
ObjectOutputStream ObjectOutputStream=新的ObjectOutputStream(新文件OutputStream(“test.bin”);
objectOutputStream.writeObject(peopleList);
}
public static void loadList()引发IOException,ClassNotFoundException{
ObjectInputStream ObjectInputStream=新ObjectInputStream(新文件输入流(“test.bin”));
Person.peopleList.addAll((List)objectInputStream.readObject());
}
公共静态void main(字符串[]args){
App App=新App();
人员p=新人员(“Sean2”,24);
试一试{
存储列表(Person.peopleList);
}捕获(IOE异常){
System.out.println(e.getMessage());
}
app.updateData();
app.setVisible(真);
}
}
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;
    }
}
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);
    }
}