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 how can I find the location of the jar file?
But if you know a better solution for something like this, I would like to hear some of them.
EDIT: I'm using mac and I want to run the same application in windows. I could write something in the System.getProperty("user.home") directory, but I want to keep the system clean, if the user decides to remove the app. There is no a better way to store the settings file, for example in the same directory with the application?
You can use a text editor such as Notepad. Once you've added the text you want to save, select File > Save and use the . ini extension in the file name. Also, choose All Files from the Save as type drop-down menu.
An INI file is a configuration file for computer software that consists of a text-based content with a structure and syntax comprising key–value pairs for properties, and sections that organize the properties.
Learn how to work with INI (configuration files) in Java. The configuration files in INI format allows the user to easily modify aspects of applications dinamically through a file. In this article, we'll show you how to use the Ini4j library to parse and write to INI file easily in Java.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With