1

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;
              }
Apurv
  • 3,723
  • 3
  • 30
  • 51
Shane Reen
  • 21
  • 5

1 Answers1

0

You can just write to an 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;
}
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
  • Where would i implement these methods? In a button or in the main. I've tried add both to the main and both to the button but when this is done, a serialized file is never created? – Shane Reen Apr 26 '13 at 16:28
  • I don't know the architecture of your application, but I guess: `onLoad()` should be called in the `main()` method, `onSave()` whenever you quit the application. See [this answer](http://stackoverflow.com/a/2361567/1277252). – Moritz Petersen Apr 27 '13 at 13:44