I've created an onLoad method to use serialization in my program but i want to use an onSave method along with this so as the program is closed and started up again, I wont have to populate my Jlists again and again.
I've tried create my own onSave function but haven't been able to get it anywhere near working.
Can somebody please show me an example or give me the onSave function to make my serialization work efficiently.
Here is my onLoad() method :
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();
}
}
Here is my attempt at the onSave method:
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;
}