Java Save and Load Program's State

7k Views Asked by At

in my java Project I have several classes/java files but is in Menu class that is stored all the lists of stuff that is used. In terms of data I store 6 Lists(2 ArrayLists and 4 HashMaps) which 1 is defined in Menu class and others are in different classes. So I need to create a savestate and a loadstate to when I close the program to restore the previous state. All the Lists are implemented with Serializable

Is it possible to save all the Menu's state and reload it or I've to save all the lists individually? Save all in one file would be great.

Here is the function I have, works(no warnings/errors) and compiles but doesn't creates the file "datafiles".

Any ideas?

    private void MenuSave(){
    String wd = System.getProperty("user.dir");

    JFileChooser fc = new JFileChooser(wd);
    int rc = fc.showDialog(null, "Select Data File Location to Save");

    if (rc == JFileChooser.APPROVE_OPTION)
    {
    File file = fc.getSelectedFile();
    String filename = file.getAbsolutePath();

    savestate(lf, lc, lv, lcl,filename);}
    }


public void savestate(Cars la, Bikes l2, Motos l3, Planes l4, People n1, Food n2, String filename){

    int i;
    File out = new File(filename);

    ObjectOutputStream output = null;

    try{
        output = new ObjectOutputStream(new FileOutputStream(filename));
        for(Car c : la.getCars().values()){
            output.writeObject(c);
        }
        for(Bike b : l2.getBikes().values()){
            output.writeObject(b);
        }
        for(Moto m : l3.getMotos().values()){
            output.writeObject(m);
        }
        for(i=0;i<n1.size();i++)
        {output.writeObject(n1.get(i)));
        }
        for(i=0;i<n2.size();i++)
        {output.writeObject(n2.get(i)));
        }


    }catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (output != null) {
                output.flush();
                output.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
2

There are 2 best solutions below

0
On BEST ANSWER

So as I thought I just need to save the lists individually without that for . 1-Choose where to save the file, then save the Classes in there. 2-To read just parse the input and store replacing the current Classes. ...

    String wd = System.getProperty("user.dir");
    this.setAlwaysOnTop(false);
    JFileChooser fc = new JFileChooser(wd);

    fc.setDialogType((int)JFileChooser.SAVE_DIALOG);


    int rc = fc.showDialog(null, "Select Data File");
    this.setAlwaysOnTop(true);

    if (rc == JFileChooser.APPROVE_OPTION)
    {
    File file = fc.getSelectedFile();

    ObjectOutputStream output = null;

    try{
    output = new ObjectOutputStream(new FileOutputStream(file));
    output.writeObject(list1);
    output.writeObject(list2);
    output.writeObject(list3);
    ....


    output.close();

    }catch (IOException x){
     ....
    }catch(NullPointerException n){
     ....    
    }}

to read is just the same:

    String wd = System.getProperty("user.dir");
    this.setAlwaysOnTop(false);
    JFileChooser fc = new JFileChooser(wd);
    fc.setDialogType((int)JFileChooser.OPEN_DIALOG);
    int rc = fc.showDialog(null, "Select Data File to Load");
    this.setAlwaysOnTop(true);

    if (rc == JFileChooser.APPROVE_OPTION)
    {
    File file = fc.getSelectedFile();
    String filename = file.getAbsolutePath();


    ObjectInputStream input = null;
    try{
    input = new ObjectInputStream(new FileInputStream(file));
    this.list1=(ListType1)input.readObject();
    this.list2=(ListType2input.readObject();
    ....
    }catch (IOException x){
      ...  

    }catch(ClassNotFoundException x){
      ...
    }
    }
4
On

doesn't creates the file "datafiles".

I'll bet that it does, just not where you are expecting to find it. Don't "drop your files wherever they fall", put them some place that is read/writable, logical and reproducible.

String filename = "datafiles";
File out = new File(System.getProperty("user.home"), filename);
// ...
    output = new ObjectOutputStream(new FileOutputStream(out));

Then look in user home for the datafiles (why does it have no file type/extension?) file.

  1. The File constructor that accepts 2 String (parent & name) parameters uses the correct File separator for the OS.
  2. user.home is a system property that points to a stable, reproducible path that has read/write access.