Java 保存和加载程序的状态

问题描述:

在我的 java 项目中,我有几个类/java 文件,但是在 Menu 类中,该类存储了所有使用的东西列表.在数据方面,我存储了 6 个列表(2 个 ArrayLists 和 4 个 HashMaps),其中 1 个在 Menu 类中定义,其他在不同的类中.所以我需要在关闭程序时创建一个 savestate 和一个 loadstate 来恢复以前的状态.所有列表都使用 Serializable

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.

这是我拥有的函数,可以工作(没有警告/错误)并编译但不创建文件datafiles".

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

有什么想法吗?

    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();
        }
    }
}

所以我认为我只需要单独保存列表而不需要 for .1-选择保存文件的位置,然后将类保存在那里.2-读取只是解析输入并存储替换当前类....

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){
     ....    
    }}

阅读是一样的:

    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){
      ...
    }
    }