Is there a way for my application to figure out where in the file system it is installed, and then read/write safely?
You can maybe read the user.dir
system property to get the path that the application was started in. Note that user.dir
is a read only property, i.e. you can't change the "current directory" by setting the user.dir
property. You read system properties with the System.getProperty(String)
method. This is not exactly the same thing as "installed in" but it may work. But it's kinda weak.
If really you want the location of the install directory, either force the user to set an environment variable (MYAPP_HOME) or scan the whole file system. Personally, I don't like these options.
Actually, and if the data are user specific, the best choice in my opinion would be to read/write data in the user home directory (use the system property user.home
to get it), for example in something like ~/.yourapp
(Windows users never go in their %USER_HOME%
anyway) or, even better, in a directory following Freedesktop XDG Base Directory Specification (obviously, only Linux users would care of that).
Then, to read/write, just use the java.io.File
which is cross-platform when used properly (e.g. use File.separator
if you need to build a path, don't use an hard coded version of the name-separator).