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

1

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

I have a HashMap with approximately 10 values (others may appear over time) that represent user preferences. The problem is that Preferences does not allow storing a Map , so I would have to go through it by inserting each String to the 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 get a "ugly" thing on record by the amount of keys. In Map , for me it is normal but I do not know how it will be in the reg system because I have never worked with it before. And here comes my doubts:

  • Should I use the S.O. registry to store such information? By "that kind," consider basic information about an application. If the answer is no, when should I make use of the system registry?

  • As mentioned before, new preferences may appear with the time. It is best to abandon the idea of saving in the registry and file (.txt) with preferences?

  • asked by anonymous 26.01.2015 / 13:54

    2 answers

    1

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

    Answers:

      
  • Should I use the S.O. registry to store such information? By "that kind," consider basic information about an application.
  •   

    No, because:

    • The Windows registry is not intuitive for the user to configure 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 to the registry, not even the user area.

      / li>
    • Your system probably already uses the database so you are already much better equipped to use this feature. Java also offers satisfactory mechanisms for writing property files or custom text files (serialization of objects for text, JSon ...)

      

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

    Maybe to provide a very underused configuration and make sure you keep obscure so it does not attract experimental user changes.

    Saving application definitions to text file

    Windows offers a specific folder for application data, which is AppData . It is under the user's folder and the user is always allowed to write there (other than the Program Files folder, 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 good thing about writing the application settings here is that they are not obvious to the user, so they do not invite you to change them or waste time worrying about them.

    Time: "user is allowed to write" is the same as the "write permission" in case the application runs under the user's credentials.

        
    26.01.2015 / 14:41
    1

    You could think of following solutions:

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

    I believe that these alternatives will give you an easier solution to implement, as 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 it.

        
    26.01.2015 / 14:22