在 Java 应用程序中保存数据的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/513343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:36:54  来源:igfitidea点击:

Best way to save data in a Java application?

javadatabasesettingsstate

提问by fabiopedrosa

I'm trying to find the best way to save the state of a simple application. From a DB point-of-view there are 4/5 tables with date fields and relationships off course.

我试图找到保存简单应用程序状态的最佳方法。从数据库的角度来看,有 4/5 的表带有日期字段和关系。

Because the app is simple, and I want the user to have the option of moving the data around (usb pen, dropbox, etc), I wanted to put all data in a single file.

因为该应用程序很简单,而且我希望用户可以选择移动数据(USB 笔、保管箱等),所以我想将所有数据放在一个文件中。

What is the best way/lib to do this?

这样做的最佳方法/库是什么?

XML usually is the best format for this (readability & openness), but I haven't found any great lib for this without doing SAX/DOM.

XML 通常是最好的格式(可读性和开放性),但我没有找到任何很好的库,而不做 SAX/DOM。

采纳答案by Peter ?tibrany

If you want to use XML, take a look at XStreamfor simple serialization of Java objects into XML. Here is "Two minute tutorial".

如果您想使用 XML,请查看XStream以将 Java 对象简单地序列化为 XML。这是“两分钟教程”

If you want something simple, standard Java Properties formatcan be also a way to store/load some small data.

如果你想要一些简单的东西,标准的Java 属性格式也可以是一种存储/加载一些小数据的方式。

回答by Steve B.

2 other options you might consider -

您可能会考虑的其他 2 个选项 -

  • Hsqldb is a small sql db written in java. More relevant for your purposes, it can be configured to simply write to a csv file as it's data store, so you could conceivably use it's text output as a portable datastore and still use sql, if that's what you prefer.
  • A second option might be to write the datastore directly to a serialized file either directly or through a library like prevayler. Very good performance and simple to implement, cons are the fragility and opacity of the format.
  • hsqldb 是一个用java编写的小型sql db。与您的目的更相关的是,它可以配置为简单地写入 csv 文件,因为它是数据存储,因此您可以想象使用它的文本输出作为便携式数据存储并仍然使用 sql,如果这是您喜欢的。
  • 第二种选择可能是直接将数据存储直接写入序列化文件,或者通过类似 prevayler 的库直接写入序列化文件。非常好的性能和简单的实现,缺点是格式的脆弱性和不透明性。

But if the data is small enough, xml is probably much less bother.

但是如果数据足够小,xml 可能就不那么麻烦了。

回答by Rahel Lüthy

consider using plain JAXB annotations that come with the JDK:

考虑使用 JDK 附带的普通 JAXB 注释:

@XmlRootElement  
private class Foo {  
    @XmlAttribute  
    private String text = "bar";
}

here's a blog-post of minethat gives more details on this simple usage of JAXB (it also mentiones a more "classy" JAXB-based approach -- in case you need better control over your XML schema, e.g. to guarantee backwards compatibility)

这是我的一篇博客文章,它提供了有关 JAXB 的这种简单用法的更多详细信息(它还提到了一种更“经典”的基于 JAXB 的方法——以防您需要更好地控制 XML 模式,例如保证向后兼容性)

回答by Marco Lackovic

If you don't need to provide semantic meaning to your data then XML is probably a wrong choice. I would recommend using the fat-free alternative JSON, which is much more naturally built for data structures.

如果您不需要为数据提供语义,那么 XML 可能是一个错误的选择。我建议使用无脂肪替代JSON,它更自然地为数据结构构建。