【问题标题】:OnSave method to work with onload method?OnSave 方法与 onload 方法一起使用?
【发布时间】:2013-04-20 09:41:22
【问题描述】:

我已经创建了一个 onLoad 方法来在我的程序中使用序列化,但我想同时使用一个 onSave 方法,以便在程序关闭并再次启动时,我不必一次又一次地填充我的 Jlist。

我尝试创建自己的 onSave 函数,但无法在任何地方使用它。

谁能给我一个例子或者给我onSave函数来让我的序列化工作有效。

这是我的 onLoad() 方法:

private void onLoad()
    {//Function for loading patients and procedures
        File filename = new File("ExpenditureList.ser");
        FileInputStream fis = null;
        ObjectInputStream in = null;
        if(filename.exists())
        {
            try {
                fis = new FileInputStream(filename);
                in = new ObjectInputStream(fis);
                Expenditure.expenditureList = (ArrayList<Expenditure>) in.readObject();//Reads in the income list from the file
                in.close();
            } catch (Exception ex) {
                System.out.println("Exception during deserialization: " +
                        ex); 
                ex.printStackTrace();
            }
        }

这是我对 onSave 方法的尝试:

try
              {
                 FileInputStream fileIn =
                                  new FileInputStream("Expenditure.ser");
                 ObjectInputStream in = new ObjectInputStream(fileIn);
                 expenditureList = (ArrayList<Expenditure>) in.readObject();


                 for(Expenditurex:expenditureList){
                        expenditureListModel.addElement(x);
                     }


                 in.close();
                 fileIn.close();
              }catch(IOException i)
              {
                 i.printStackTrace();
                 return;
              }catch(ClassNotFoundException c1)
              {
                 System.out.println("Not found");
                 c1.printStackTrace();
                 return;
              }

【问题讨论】:

  • 你的 onSave 方法是什么样的?
  • 是正确的sn-p吗?您正在使用 Input 流。
  • 应该是输出流。不确定我在 onSave 方法方面在做什么,希望有人可以帮助我。

标签: java serialization


【解决方案1】:

你可以只写一个 ObjectOutputStream:

public void onSave(List<Expenditure> expenditureList) {
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new FileOutputStream(new File("ExpenditureList.ser")));
        out.writeObject(expenditureList);
        out.flush();
    }
    catch (IOException e) {
        // handle exception
    }
    finally {
        if (out != null) {
            try {
                out.close();
            }
            catch (IOException e) {
                // handle exception
            }
        }
    }
}

public List<Expenditure> onLoad() {
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new FileInputStream(new File("ExpenditureList.ser")));
        return (List<Expenditure>) in.readObject();
    }
    catch (IOException e) {
        // handle exception
    }
    catch (ClassNotFoundException e) {
        // handle exception
    }
    finally {
        if (in != null) {
            try {
                in.close();
            }
            catch (IOException e) {
                // handle exception
            }
        }
    }
    return null;
}

【讨论】:

  • 我将在哪里实现这些方法?在一个按钮或主要。我尝试将两者都添加到主按钮和按钮,但是完成后,永远不会创建序列化文件?
  • 我不知道你的应用程序的架构,但我猜:onLoad() 应该在main() 方法中调用,onSave() 每当你退出应用程序时。见this answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
  • 2021-08-04
  • 2010-12-21
  • 1970-01-01
相关资源
最近更新 更多