简体   繁体   English

如何为独立的Java应用程序存储简单数据

[英]How to store simple data for standalone java application

Assume I have a long list of People with some basic characteristics: 假设我有一长串具有一些基本特征的人:

Matt, 27, male Matt,27岁,男

Anne, 49, female 安妮(49岁),女

etc... 等等...

I want to store this data locally for a standalone desktop application that does not require access to the internet. 我想将此数据存储在本地,而不需要访问Internet的独立桌面应用程序。 My current method is writing the names to a json file and reading them all in to a Person class in java when the program is opened; 我当前的方法是将名称写入json文件,并在打开程序时将它们全部读取到java中的Person类中。 however, I did not think this was efficient as the data only needs to be accessed if someone is performing a search. 但是,我认为这样做效率不高,因为仅当有人执行搜索时才需要访问数据。

So I considered a simple JSONparser, let's say searchNameInFile("Matt", "data.json") { return person; 所以我考虑了一个简单的JSONparser,比方说searchNameInFile(“ Matt”,“ data.json”){return person; }, but I did not know if this was a good way to store the data with regards to efficiency. },但我不知道这是否是存储效率方面的好方法。 I considered sqlite shortly, but I am only storing one table of data and thought it might be overkill. 我不久就考虑了sqlite,但是我只存储一张数据表,并认为这可能会过大。 What is an appropriate, efficient method for storing and searching local data? 有什么合适的,有效的方法来存储和搜索本地数据?

I really think you should reconsider SQLite. 我真的认为您应该重新考虑SQLite。

https://www.tutorialspoint.com/sqlite/sqlite_java.htm https://www.tutorialspoint.com/sqlite/sqlite_java.htm

Your data will be stored locally in a file that can be easily queried with standard Java SQL stuff. 您的数据将存储在本地文件中,可以使用标准Java SQL内容轻松查询。 It does not require any server software or an internet connection. 它不需要任何服务器软件或互联网连接。

If you don't want to use SQLite, here are two possible ways, depending on what you do care about. 如果您不想使用SQLite,则有两种可能的方法,具体取决于您关心的事情。

If you only care about time complexity, but have plenty of memory to throw around, you could store your data in an efficient structure such as a HashMap, and use Java's built in Serializable to write the HashMap out to a file, and read it back in on next start. 如果您只关心时间的复杂性,但有足够的内存可以使用,则可以将数据存储在高效的结构中,例如HashMap,然后使用Java内置的Serializable将HashMap写入文件,然后再读取回去在下次启动时。

//Where Person is a data class that implements Serializable and contains your desired fields
public void saveData(HashMap<String, Person> people) {
    FileOutputStream f = new FileOutputStream(new File("people.txt"));
    ObjectOutputStream o = new ObjectOutputStream(f);
    o.writeObject(people);
    o.close();
    f.close();
}
public HashMap<String, Person> loadData() {
    FileInputStream fi = new FileInputStream(new File("people.txt"));
    ObjectInputStream oi = new ObjectInputStream(fi);
    HashMap<String, Person> data = (HashMap<String, Person>) oi.readObject();
    oi.close();
    fi.close();
    return data;
}

If you don't mind O(n) search times, then your current solution of re-reading the file on every search is fine. 如果您不介意O(n)个搜索时间,那么当前的解决方案是在每次搜索时重新读取文件。

You can also consider another Simple in-memory DB as H2. 您还可以将另一个简单的内存数据库DB视为H2。 It will save data in a relational db that is store in your computer as a file. 它将数据保存在关系数据库中,该数据库作为文件存储在计算机中。 The advantage is the high-retrieval speed benchmark it has. 优点是它具有高检索速度基准。 H2 H2

Advantage is that configuration is as simple as create the DB object and configure it to use in-memory file. 优点是配置就像创建数据库对象并将其配置为使用内存文件一样简单。

it might be overkill 这可能是过大的

Re-inventing the wheel would be the real overkill. 重新发明轮子将是真正的杀伤力。 There are plenty libraries and methods out there for storing and loading data. 有很多用于存储和加载数据的库和方法。

Since you want to store the data locally in a file, but also want to search in the data, an embedded SQL database should be a good option for you. 由于您要将数据本地存储在文件中,而且还希望在数据中进行搜索,因此嵌入式SQL数据库对于您来说应该是一个不错的选择。 Either SQLite, h2 or HSQL. SQLite,h2或HSQL。

For acessing the data, you can use anything from plain JDBC up to Spring Data JPA 为了访问数据,可以使用从纯JDBC到Spring Data JPA的任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM