java OnSave方法与onload方法配合使用?
我已经创建了一个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;
}
共 (0) 个答案