Best way to store data between program runs in jav

2019-01-23 19:43发布

What is the best way to store data between program runs in Java? I already know that you can use a text file and store the information that way, but I was wondering if there is a better way to store the information that is generated by the program between runs of the program.

Also, is there any way to do it so as to keep the information secure? Specifically, I want to keep the end user from being able to access it.

11条回答
等我变得足够好
2楼-- · 2019-01-23 20:04

The best thing to use when beginning Java is to use an ObjectOutputStream or a text file. Once you have more experience then you can use databases.

查看更多
做自己的国王
3楼-- · 2019-01-23 20:06

How about serialization?

It can't be read by the user and it's relatively easy.

查看更多
Animai°情兽
4楼-- · 2019-01-23 20:07

I've never used it myself, but I think that's what the JDK's java.util.prefs.Preferences was originally designed for.

查看更多
我只想做你的唯一
5楼-- · 2019-01-23 20:11

Some people suggested to use serialization. Beware that there are a number of disadvantages to serialization.

  • The versioning problem. If you change something in the classes that are serialized, then serialized files written with the old version of your program can't be read easily anymore.
  • You don't know the exact file format. It will be really hard if you want to write a different program later, possibly in a different programming language, that needs to read the file.

Serialization is not well-suited for long-term storage.

I would suggest using a small, embedded database instead. (An embedded database is a database that runs in the same process as your program). Note that Sun's Java includes Java DB, which is a version of Apache Derby. There's also HSQLDB, which is another small and pure Java database that can be used as an embedded database.

查看更多
SAY GOODBYE
6楼-- · 2019-01-23 20:11

XML as a serialization technique is more resilient to future changes in your program that will adjust the storage than binary formats such as Object Serialisation. However that would make it very readable and changeable by most users.

A very simple compression/decompression would stop almost all users from getting at the actual contents of the data. The use of GZipInputStream/GZipOutputStream around your current writing stream will do the job. The more elaborate your defence against prying you get the more it will impact the users of your software.

查看更多
Rolldiameter
7楼-- · 2019-01-23 20:12

As others have said, there are a myriad of ways to serialize data. You can use something lightweight like SQLite or just plain serialization. Just realize that any attempts you make to encrypt the data can be defeated, especially in the case of Java code since it can be easily reversed.

However, if the bulk of your users are not technical enough to understand the complexities of reverse engineering a Java program to figure out how to decrypt your data, you should be able to get away with some basic encryption methods like what was mentioned in another answer and be good. Just realize that anytime anything resides on a machine you don't control, there is no way to keep the most persistent users from figuring out how to crack it.

I personally would suggest using sqlite and using some simple encryption on the data you put in the fields so if someone is smart enough to be able to connect to the local DB file, they still have to reverse your crypto algorithm in some manner. 99.9% of regular users won't bother with this level of investigation.

查看更多
登录 后发表回答