如何将序列化文件加载回 arrayList [英] How to load a serialized file back to an arrayList

查看:21
本文介绍了如何将序列化文件加载回 arrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过一些关于这个问题的问题,但无法解决我的问题.

ive looked at a few problems on this matter already but couldnt resolve my issue.

如下所示,我将 Patient ArrayList(pList - 在类的顶部是私有的)中的所有对象保存到Patient.ArrayList"中.ser"文件通过序列化.据我所知,这是没有问题的."patSizeAtSave" 是一个私有变量,我用来标记为加载文件时使用的绑定(见下文)

As you can see below, i save all the objects in my Patient ArrayList(pList - which is private at the top of the class) into the "Patient.ser" file through serialization. As far as i can tell this is working with no problems. "patSizeAtSave" is a private variable which i am using to mark as a bound to work with when loading the file(see below)

patModel"是我在 GUI 中用于 JListsDefaultListModel 所以我尝试使用已添加回 Patient ArrayList(pList)

the "patModel" is the DefaultListModel i use for the JLists in my GUI so i try to populate those lists with what has been added back to the Patient ArrayList(pList)

我的问题:当我点击 GUI 上的加载按钮时,它会调用下面的 loadPatientList() 方法,但没有放置任何东西进入我的 JLists,所以我不知道它是否有效.

MY PROBLEM: When i hit the load button on the GUI it calls the loadPatientList() method below, but nothing is being put into my JLists so i can't tell if it works at all.

知道如何解决这个问题吗?

Any idea's on how to fix this?

private void savePatientList() throws FileNotFoundException {
    try
    {
    FileOutputStream fs = new FileOutputStream("Patient.ser");
    ObjectOutputStream os = new ObjectOutputStream(fs);
    for(Patient p: pList)
    {
        os.writeObject(p);
    }
    fs.close();
    os.close();
    patSizeAtSave = pList.size();
    JOptionPane.showMessageDialog(jTabPane, "Save Complete!", "Notification", JOptionPane.INFORMATION_MESSAGE);
    } 
    catch (IOException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    } 

}
private void loadPatientList()
{
    FileInputStream fs;
    ObjectInputStream is;
    try
    {
    fs = new FileInputStream("Patient.ser");
    is = new ObjectInputStream(fs);

    for(int i = 0; i < patSizeAtSave; i++)
    {
        Patient p = (Patient) is.readObject();
        pList.add(p);
        patModel.addElement(p.getPNo() + ": " + p.getPName());
    }
    jListPatient.setModel(patModel);
    jListPatient2.setModel(patModel);
    fs.close();
    is.close();
    } 
    catch (FileNotFoundException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

推荐答案

您是否尝试在保存后将患者列表加载到同一个 JVM(不退出 JVM)中?因为如果您在重新加载患者列表之前退出 JVM,您的 patSizeAtSave 将被重置为 0(因为它没有保存到任何文件/永久存储),而只会在内存中.当您在新的 JVM 中调用 loadPatientList 时,您的 for 循环将不会加载任何患者,因为 patSizeAtSave 为 0.

Did you try to load you patients list in the same JVM (without exiting the JVM) after saving it ? Because if you exit your JVM before reloading you patients list, your patSizeAtSave will be reset to 0 (since it is not saved to any file/persitent storage) but only in memory. When you will call loadPatientList in a new JVM, your for loop will not load any patient since patSizeAtSave is 0.

您应该保存整个列表 pList(假设您使用 List 的 Serializable 实现,例如 ArrayList/LinkedList),如下所示:

You should rather save the whole list pList (assuming you use a Serializable implementation of List, sucha as ArrayList / LinkedList), like this :

FileOutputStream fs = new FileOutputStream("Patient.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(pList);

然后用 :

pList = (List<Patient>) is.readObject();
for(Patient p: pList)
{
    patModel.addElement(p.getPNo() + ": " + p.getPName());
}

这样,列表大小也将保存在您的序列化文件中.

This way, the list size will also be saved in you serialized file.

问候

这篇关于如何将序列化文件加载回 arrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆