When should I save my application data to the system registry?

Asked

Viewed 537 times

1

I need to store some information of my application for when the user starts it again to be loaded the data of the last use. At first I thought about using the Windows registry (in this case I’m sure the application will only run in Win environment), then I looked for how to do and found the class Preferences.

I have a Hashmap with approximately 10 values (it may be that over time others appear) that represent the user’s preferences. The problem is that Preferences does not allow storing a Map, then I would have to go through it by inserting each String to preferences:

// Map de preferências
Map<String,String> prefsMap = new HashMap<>();
prefsMap.put("last_report", "10/10/1950");
prefsMap.put("last_dir", "C:\\Users\\UserName\\");
prefsMap.put("splash", "false");
prefsMap.put("load_style", "true");
// outros 6 valores...

// Inserindo no Registro
Preferences prefs = Preferences.systemNodeForPackage(Main.class);
for(Map.Entry<String,String> each : prefsMap.entrySet())
   prefs.put(each.getKey(), each.getValue());

Looking at this code, it looks like it’s going to be an "ugly" thing on the record by the amount of Keys. No Map, for me it’s normal but I don’t know how it will look in the reg system by never having worked with it before. And here comes my doubts:

  1. Should I use the S.O. registry to store information of this type? For "this type," consider basic information for an application. If the answer is no, when then should I make use of the system record?

  2. As stated before, new preferences may appear with the time. It is best to abandon the idea of saving to the registry and create a file (.txt) with preferences?

2 answers

1

You could think of following solutions:

  • Use an embedded database with the HSQLDB and the H2.
  • If there is really little information, save it in a text file in the format of .properties.

I believe that these alternatives will give you an easier solution to implement, because there is no need to maintain system records. In addition, its solution is independent of the Operating System, which is always a good feature of the same.

  • But as I said: The platform will be Windows and this is guaranteed. My doubts are at point 1 and 2 of the question.

  • Despite the multiplatform, condorco with the use of databases and text files. + 1

  • I don’t see this amount of data in the windows registry as long as it is transparent to the user (it doesn’t have to manage these records). I believe that there are simpler and more robust solutions as demonstrated in the answer. The problem of using windows logs is that, back and forth, they create unexpected problems.

  • @Eduardofernandes thank you, helped a lot. I searched and found some disadvantages after reading your comment.

  • Dear @rrnan, if the answer is correct for your question, please mark it by clicking on the "check" sign. Thank you.

1


Using the Windows registry is more complicated than using text file or embedded database.

Answers:

  1. Should I use the S.O. registry to store information of this type? For "of this type," consider basic information for an application.

No, because:

  • The Windows registry is not intuitive for the user to set up as much as a text file.

  • There are environments where the user is not allowed to use the Windows registry editor, and there are still environments where applications are not allowed to write in the registry, not even in the user area.

  • Your system probably already uses a database so you are already much better equipped to use this resource. Java also offers satisfying mechanisms for writing to property files or custom text files (text object serialization, Json...)

If the answer is no, when then should I make use of the record of system?

Perhaps to provide a very little used configuration that you make a point of keeping obscured so it doesn’t attract experimental user changes.

Saving application settings in text file

Windows offers a specific folder for application data, which is the Appdata. It is under the user folder and the user is always allowed to write there (different from the folder Program Files, for example).

To get this folder:

// retorna algo como "C:\Users\caffé\AppData\Roaming"
String pastaDadosDoAplicativo = System.getenv("APPDATA");

In it you can create a subfolder for your own application.

Another positive point of writing the application settings there is that they are not evident to the user, so they do not invite you to change them or waste time worrying about them.

In time: "user being allowed to write" is the same as "application being allowed to write" in case the application runs under user credentials.

  • Caffé, I do not use database because it is an application desktop which does not store information. But I, I, am intending to save some data for when the user runs it again not to be with the face of "first time". To be clear: It’s customization data only, so I don’t see much advantage in using an embedded BD to store 10~12 strings. ps: I do not understand the part of being intuitive for the user, are you assuming that the user will have access and will be able to edit this information? This data is important only to modify the appearance of the application.

  • @rrnan It would be interesting to be intuitive if the user needed to change. As for the user have access to change, is not at your control neither saving in text file nor saving in Windows registry - both, by default, the user can change; unless you encrypt. Tip: go to text file :-)

  • @rrnan I added a hint where to store the text file.

  • 1

    Man, I really enjoyed saving on %Appdata. Your previous comment also gave me a light, maybe I create something encrypted so the user does not poke around and a file for him to modify if he thinks necessary. I’m gonna run some tests...

  • 1

    I ended up doing it this way, I noticed that I was wanting to complicate too much, hehe. I appreciate the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.