【问题标题】:Java Save and Load Program's StateJava 保存和加载程序的状态
【发布时间】:2012-05-27 00:26:39
【问题描述】:

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

【问题讨论】:

  • 您可能希望在代码中添加 println 语句,以查看调用了哪些代码以及这些调用时变量的状态。
  • 为什么不使用java.util.Preferences
  • @trashgod Preferences 可能是更好的选择,尽管我们还没有足够的了解来确定。请参阅下面我对 mKorbel 的评论。

标签: java swing savestate


【解决方案1】:

不创建文件“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.home 是一个 system property,它指向具有读/写访问权限的稳定、可重现的路径。

【讨论】:

  • 偏好,由@trashgod 提及
  • @mKorbel 我认为Preferences 纯粹是用于String 或“简单”对象类型Long/Float 的值,而不是ObjectOutputStream 所暗示的通用Object 值。 OTOH,也许OP可以将每个对象的属性存储为那些基本类型。 OP 需要更多输入 - 类的定义将非常有帮助。
  • @AndrewThompson:我喜欢user.home,但是Preferences可以存储复杂类型;更多here.
  • 这样,它确实在用户文件夹中创建了 4 字节的文件。但是有一个问题,它只会在所有列表都为空的情况下创建,如果我将元素添加到列表中,它会给出异常 NullPointerException 。然后我需要处理负载......
【解决方案2】:

所以我认为我只需要单独保存列表而不需要 for 。 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){
      ...
    }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2013-11-02
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多