【问题标题】:how can I save variable in a file?如何将变量保存在文件中?
【发布时间】:2013-07-07 19:32:40
【问题描述】:

有没有一种方法可以保存变量,例如颜色数组,然后检索它。我正在制作棋盘游戏,我需要能够在任何给定时间保存。如果它不能那样工作,任何人都可以给我任何其他建议我可以使用什么?

【问题讨论】:

  • 你应该看看序列化。
  • 序列化对于像这样的简单任务来说可能有点太重了。看看javafile i/o。我必须 -1 因为这个问题可以通过简单的谷歌搜索来解决。对不起。如果您确实表示类似序列化的意思,请编辑帖子,我会收回它。
  • 这一切都取决于您要保存哪个元素的数组。如果元素是可序列化的,那么您可以使用序列化。否则请使用 Curt 的回答。

标签: java arrays io persistence


【解决方案1】:

您可以使用ObjectOutputStream 来编写实现Serializable 接口的对象。

FileOutputStream fos = new FileOutputStream("my.save");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(colorsArray);

当然,同样的加载方式也是可以的:

FileInputStream fis = new FileInputStream("my.save");
ObjectInputStream ois = new ObjectInputStream(fis);
colorsArray = (Color[]) ois.readObject();

【讨论】:

  • +1 你也应该提到原语的 DataInputStream 和 DataOutputStream。
  • 我认为ObjectOutputStream 提供了与DataOutputStream 相同的编写原语的方法。但是如果OP不需要额外的能力,那么轻量级的DataOutputStream当然也适合写存档数据。
  • 啊,我没有意识到这一点。
【解决方案2】:

查看 XML 序列化实用程序。如果你的“变量”是一个类实例(或包含在一个类实例中),这应该使保存值变得非常简单。

如果不是,你必须想办法写出变量的值,然后从字符串中解析回来,这样你就可以将它保存到文本文件中。

【讨论】:

    【解决方案3】:

    序列化数据的方法有很多种。这是一个(从我打开的一个小项目中提取的),使用ArrayList 作为数据容器,XMLEncoder/XMLDecoder 用于序列化,并将它们放在一个 Zip 中以备不时之需。

    public void loadComments() throws FileNotFoundException, IOException {
        File f = getPropertiesFile();
        FileInputStream fis = new FileInputStream(f);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry entry = zis.getNextEntry();
    
        while (!entry.getName().equals(COMMENTS_ENTRY_NAME)) {
            entry = zis.getNextEntry();
        }
        InputSource is = new InputSource(zis);
        XMLDecoder xmld = new XMLDecoder(is);
        comments = (ArrayList<Comment>) xmld.readObject();
        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(CommentAssistant.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void saveComments() throws FileNotFoundException, IOException {
        CommentComparator commentComparator = new CommentComparator();
        Collections.sort(comments, commentComparator);
    
        File f = getPropertiesFile();
        System.out.println("Save to: " + f.getAbsolutePath());
        File p = f.getParentFile();
        if (!p.exists() && !p.mkdirs()) {
            throw new UnsupportedOperationException(
                    "Could not create settings directory: "
                    + p.getAbsolutePath());
        }
        FileOutputStream fos = new FileOutputStream(f);
        ZipOutputStream zos = new ZipOutputStream(fos);
    
        ZipEntry entry = new ZipEntry(COMMENTS_ENTRY_NAME);
        zos.putNextEntry(entry);
    
        XMLEncoder xmld = new XMLEncoder(zos);
        xmld.writeObject(comments);
        xmld.flush();
        xmld.close();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 2016-10-25
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多