2

So far I have seen that I can save them via these:

  1. The Preferences class - I like it but it requires admin rights in windows for the registry editing
  2. The Properties class - This seems that it is useful for storing data. I also saw that I can load a file from the jar like this:

inputStream = Main.class.getResourceAsStream("config.properties");

however - once I load it I ( please correct me if I am wrong ) CANNOT edit it. Editing text file inside the jar programmatically is not possible. Instead I should just save it to the same directory as the .jar using this:

File newFile = new File("config.properties");
OutputStream os = new FileOutputStream(newFile);
properties.store(os, "Properties File");

as long as the file is where the class called Main is.

  1. The third way is basically the same as the second but while loading from hard coded default values instead loading from txt file.

Please tell me if you know another way of loading settings in a program. And please clarify this - basically the only way the user doesn't get a pesky file next to his jar for saving properties is for me to use Preferences. Of course taking under consideration that we can't have absolute paths seeing as different operating systems will be a problem.

5
  • You could also save some state to the database. Commented Sep 7, 2015 at 1:09
  • Rather than save the properties file next to the JAR, save it inside AppData on Windows (System.getenv("APPDATA")) or inside the user home on Unix-based systems (System.getProperty("user.home"), or ~/ if the home property is null). You may want to create a method that detects if the operating system is Windows and returns the data directory path appropriately. Commented Sep 7, 2015 at 1:13
  • @TimBiegeleisen I didn't really think of using databases because all I have to save is 2 options that once the user set stay default and only change if he changes them and not reset on every restart with defaults. How do databases fare with OS support? I'm not really familiar with how they work on local level. Do they require additional software for a user to use a program that reads/writes in them? Commented Sep 7, 2015 at 1:14
  • @TsvetanDimitrov using file properties is a very well way for me, i save settings of a java applications in the user's folder (and is portable in different enviroment), inside another folder with the name of my app Commented Sep 7, 2015 at 1:15
  • @Vulcan I did think about saving in APPDATA but then I noticed some machines save their environment variable for appdata differently. I saw some info about the user.home but I thought only windows has that? Am I wrong? Commented Sep 7, 2015 at 1:16

2 Answers 2

2

Apache has a nice commons configuration library for handling configuration: https://commons.apache.org/proper/commons-configuration/ This would be good if you'll eventually need a lot more configuration or if you'll maintain several applications and want a common way to handle different kinds of configuration files.

But if all you're looking for is to store a couple properties per user somewhere, I would just use the Properties class and store each respective user's data within a file under their user.home directory called your app name.properties. That way one user's data does not collide with another's, and the chances the user would have write privileges in user.home is pretty good. User.home is definitely available in more systems than windows - it's set for linux/unix envirionments, so that would cover Mac OSX as well.

If you don't already have a database, I wouldn't look to introduce one just to store a couple user preferences.

Sign up to request clarification or add additional context in comments.

Comments

1
  • regedit.exe does not require Administrator permission as long as the user is editing his/her own Java Preferences.
  • If your Properties files were to contain configurations, save them outside the jars. Preferably in the current working directory or System.getProperty("user.home").
  • Apache Commons Configurations: https://commons.apache.org/proper/commons-configuration/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.