Java保存和加载程序的状态

时间:2012-05-27 00:26:40

标签: java swing savestate

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

实现

是否可以保存所有菜单的状态并重新加载,或者我是否可以单独保存所有列表?将所有内容保存在一个文件中会很棒。

这是我的功能,工作(没有警告/错误)和编译,但不会创建文件“ 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();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

  

不会创建文件“datafiles”。

我敢打赌它确实如此,而不是你期望找到的地方。不要“将文件丢弃到任何地方”,将它们放在一个可读/写,逻辑和可重复的地方。

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

然后在用户主页中查找datafiles(为什么它没有文件类型/扩展名?)文件。

  1. File constructor that accepts 2 String (parent & name) parameters为操作系统使用正确的File分隔符。
  2. user.homesystem property,指向具有读/写访问权限的稳定,可重现的路径。

答案 1 :(得分:0)

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

    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){
      ...
    }
    }
相关问题