没有SQL或任何Database Sever,在Java应用程序中保存数据的最佳方法是什么?

时间:2018-02-21 23:52:35

标签: java netbeans persistence javadb

我想创建一个需要存储数据的桌面应用。此应用程序可用于各种电脑。所以,我不想安装sql server和更多的应用程序来实现这一点。我想创建一个包含所有存储数据的文件,使备份更容易并且安装快速。 我认为使用JSON或XML文件,或Excel(.xmls),但这似乎很难执行搜索或其他操作。 所以,在实现之前我想要一个意见。

1 个答案:

答案 0 :(得分:1)

您可以使用com.typesafe.config或SQLite。它取决于您必须存储的数据。 另一种解决方案是使用GSON或xstream

将数据类序列化为XML \ JSON

Gson示例:

public static class Entity {
    volatile int id;
    String name;
    transient long random;

    public Entity(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
//Create new entity
Entity entity = new Entity(100, "name");
entity.random = 1234;

//Serialize to JSON. Then you can save string to file
String json = gson.toJson(entity); // {"id":100,"name":"name"}
// On application start you can deserialize your entity from file
Entity read = gson.fromJson(json, Entity.class);
相关问题