Best location for properties file - java

I'm working on a small java application that needs to load/save configuration properties. At first I tried using a properties file that lived inside the jar but I was concerned if someone were to upgrade to a later version (replace the existing jar) they would loose all of their settings.
My next idea was to write the configuration file to the disk and reference it but I'm not sure what location I should save to. I don't want to use the current directory because the user could move the jar between directories and forget about the configuration file.
Is there a common location for configuration property files (specifically on windows)? If so how would I access it (I assume it would be some sort of wild card character like %appdata%\my app\config.properties)?
Thanks!

You might want to look into using the Preferences API instead. It provides a lightweight and simple way of storing application and user preferences, without directly accessing the file system.

Try the java.util.prefs Preferences API... as long as you are using Java 1.4 or newer. I think that will do what you want. I wrote a little post about them a while back: http://coffeaelectronica.com/blog/2009/07/java-preferences-api/
Hope this helps.

The user.home system property provides the path to a users home directory if you want to store on the file system.
There is also the Preferences API that was added to Java to solve this exact problem.

Related

Can I use a custom made Shared Preference XML to set up application at startup?

I am wondering if Shared Preference is a clean way to do what I am trying to accomplish. I am building an application where I need to have the ability to allow a 3rd party to pre-setup the application on the phone prior a user using it. I figured one way of doing this would be for the 3rd party to supply a Shared Preference XML file and for the application to attempt to read it at start up. If there is no custom file provided the application will start in default conditions.
I understand that this can be done using a regular file as well and I could just read and parse through a file, I am just wondering if Shared Preference is the clean way of doing this.
I am still somewhat new to Android Development so I also welcome better ideas of accomplishing this.
Thank you
Once the the app is installed on a device you will not have permissions to write to any of the app's folder. You will need the app itself to install the configuration file when it is started up.
This would be to read the configuration file, which can be a properties file, and have the app write or copy the values to the sharedpreferences.
To have the app get the properties file, it could be done by downloading the file from a server or even reading it from the ExternalFiles folder of the device (For the second alternative you will need to set the corresponding permissions in the manifest, and also request permissions at runtime for local storage).
The app, upon startup, will have to check if it has been initialized with the properties file (which can be be a flag in sharedpreferences) and if not try to get hold of the properties file to do the setup).

Where should my application/service store its configuration files in windows?

In Linux, I'll usually put user configuration files in $HOME/.myapp and in Windows I'll use APPDATA. So far so good.
What about non-user specific configuration? In linux, I'd put it into /etc. Is there an equivalent in Windows? Please note I would like to have the service running before any user logs in. Also, in case it plays a role, I'm developing in Java.
Alternatively: I approaching this the wrong way?
You could use ALLUSERPROFILES as a base directory. This environment variable resolves to the C:\PROGRAMDATA folder in Windows7. Of course you need to add a specific folder for your applications
In summary, you should use the known folder: ProgramData.
To avoid hard coding of paths (and hence why I'm not providing them here) you should always retrieve the value via one of the following methods:
The %PROGRAMDATA% environment variable (you can also use %ALLUSERSPROFILE% but I consider %PROGRAMDATA% more meaningful)
For unmanaged code use SHGetKnownFolderPath, passing the FOLDERID_ProgramData.
For managed code use System.Environment.GetFolderPath, passing CommonApplicationData.
This folder is not writeable by non-admins, so depending on your requirements you'll want to create a directory for your program and set the ACLs you need at install time.
Some good information on this topic is provided in the blog post: "Where Should I Write Program Data Instead of Program Files".
For those interested in using other Known Folders, MSDN provides extensive documentation.
In Windows most "program files" for an app go in C:\Program Files\MyApp. The environment variable would be %ProgramFiles%\MyApp.

Common place for creating files on various platforms (non-user-specific)

I write cross-platform java app and I need some place where I can store some amount of files. These files will be used by another java application that may run under another user. This mean that I cannot use:
System.getProperty("user.home");
since I may have no permissions to read/write these files. What I need is some way to get non-user-specific folder where every app can create/read/delete files (something like "C:\ProgramData" for windows).
Is there a cross-platform way to get such folder(at least for Windows and Linux), or if there is no any - what is the best way to get such folders for Windows(should work on XP-7), Linux and Android.
Any pieces of puzzle are welcomed.
I'm not aware of such such a cross-platform folder which is additionally readable by all users. But you can:
Define a specific folder for each OS, commons-lang may help you determining the platform (see SystemUtils)
Check if the folder read/writeable for the current user during application start-up.
Using a central configuration (where the data exchange folder is defined for this installation) may also be an option, but this depends on the packaging of your project.

Does Java Web Start give a default program folder?

For Java Web Start is there a default place to store and access data related to my program? Or do I need to create a folder? For Java Web Start (assuming I don't get a program folder) is it standard to just create on in Program Files for window, Applications for mac, etc?
I would use a subdirectory in the users home directory. E.g. System.getProperty("user.home") + File.separator + ".myapp/"
But for that the user has to add extra permissions for the a web start application.
To persist you can use a properties file or XmlEncoder which are included in the JDK. Or use external libraries like XStream, Xvantage or the simple framework where it is simple as
xstream.save(anyObject)
In addition to the Preferences API for storing user settings there are a few services that can be found in the javax.jnlp package.
For your concrete requirement the PersistenceService would be particularly useful.
Alternatively you can simply provide all data that your application requires as part of your .jar files, reference them in your .jnlp file and customize how and when they are downloaded by using the DownloadService.
There is no specific default place to store and access data related to your program with webstart. However Java does have the Preferences API to provide a platform independant way of storing configuration without worrying about the specific storage location/format.

Storing a file in the user's directory in cross-platform Java

I have a Java application that runs on Mac and Windows, directly off a CD/DVD with no installation. Now, I need to store a file containing per-user data (think favourites etc.) somewhere on the local file system, so that it can be written to.
So, where do you think is a good location for this file? I am thinking something like:
<USER_DOCUMENTS_AND_SETTINGS>/application data/myapp/favourites.db for windows
<USER_HOME_DIR>/.myapp/favourites.db for mac/nix
Thoughts?
And does anyone know the best way to determine these paths within Java?
System.getProperty("user.home")
As well as user.home, the following is useful for more temporary file storage:
System.getProperty("java.io.tmpdir")
Use the Java Preferences API, designed specifically to store user preferences values in a platform independent way we all like. Java will take care of saving them and retrieving from the file or other backing store depending on the OS.
These tutorials can get you going
You can consider storing the data at Roaming directory under Windows.

Categories

Resources