How to create an .INI file to store some settings in Java?

后端 未结 6 1211
悲&欢浪女
悲&欢浪女 2020-12-08 01:09

I want to create an ini file to store some settings for my application. Is it a good idea to find where the jar file is located and create an ini file there? If yes, then ho

相关标签:
6条回答
  • 2020-12-08 01:47

    Typically Java programmers don't use .ini files, but .properties files (different format). You can use the java.lang.Properties class as a nice programmatic wrapper if you do.

    While you can get the location of your jar file by calling getProtectionDomain().getCodeSource().getLocation() on your class's .class member, I do not recommend that you do this.

    I would instead write the file to the System.getProperty("user.home") directory - the users' home directory, or if it is truly temporary, System.getProperty("java.io.tmpdir")

    0 讨论(0)
  • 2020-12-08 01:49

    If the settings are only written by your application (rather than edited manually), consider using the Preferences API.

    0 讨论(0)
  • 2020-12-08 01:56

    You should not be storing temp files in the install directory of an application. Remember, the user running the application may not have write access to that directory. The safest place to put stuff like that is in C:\Documents and Settings\username\Application Data\ApplicationName folder (adjusting the name as necessary).

    That said, however, I would probably store that type of stuff in the registry instead of a file on their computer. (But, that's just me.)

    0 讨论(0)
  • 2020-12-08 01:56

    It depends whether your ini needs to be human readable/writable under normal circumstances. If not, you can use a properties file rather than an ini file, and store it in the "user" directory.

    As for finding the jar file, you would have to find the ClassLoader for a class known to be loaded from the jar, check that it was the appropriate type of ClassLoader (ie that it's really been loaded from a jar), and you can extract the path from that. I can probably dig out the code to do this if that's really what you want. I wouldn't necessarily recommend it.

    EDIT The user.home property will give you the user directory, which you can safely use.

    0 讨论(0)
  • 2020-12-08 01:58

    The idea with the .properties file instead of the INI file is good. Also, if you store some sensitive data in there, you may consider encrypting it. Check this out:

    https://www.owasp.org/index.php/How_to_encrypt_a_properties_file

    or this:

    encrypt and decrypt property file value in java

    0 讨论(0)
  • 2020-12-08 02:05

    You can locate your application directory using the ClassLoader. See: Java: finding the application directory. Rather than an .INI file, use a .properties file - you can load and save this via the Properties class.

    As others have noted, you should not write user settings to your application directory. What if the user does not have write access to the application directory? What if your application is being used by multiple users on the same system at the same time? Neither of these situations are unusual, even on Windows.

    You might still want to load some settings from the application directory - perhaps the administrator has configured default settings there.

    A common convention is to save user settings to the user's home directory:

    /home/user/.eclipse
    C:\Documents and Settings\User\.eclipse
    

    Although this means you might leave stray files behind, this can be beneficial if the user re-installs the app. Document such things in a README. Here is how to create and get a reference to the directory:

    public static File getSettingsDirectory() {
        String userHome = System.getProperty("user.home");
        if(userHome == null) {
            throw new IllegalStateException("user.home==null");
        }
        File home = new File(userHome);
        File settingsDirectory = new File(home, ".myappdir");
        if(!settingsDirectory.exists()) {
            if(!settingsDirectory.mkdir()) {
                throw new IllegalStateException(settingsDirectory.toString());
            }
        }
        return settingsDirectory;
    }
    

    On unix-like operating systems, starting the directory name with a period (".myappdir") will make the directory hidden. On Windows, it will be located below My Documents, so users will not see the directory unless they go looking for it.

    0 讨论(0)
提交回复
热议问题