I did a search for existing specifications and implementations
for this.
After getting no results, I wrote a specification and
implementation myself.
Portadir intends to establich a vocabulary for directories:
http://www.purl.org/stefan_ram/pub/the_portadir_specification
Here is a preliminary publication of the class »FileSystem«
that implements some directories specified in Portadir.
http://www.purl.org/stefan_ram/java/FileSystem.java
The regular publication is expected to be part of the next
release of »ram.jar«.
I had a quick gander at the Portadir source and, while it's good, I
think it's missing some crucial bits. Allow me to elaborate on the
pointless incompatabilities between OSes for a moment.
Under linux, and indeed most unices, the conventions for storing user data are:
1. Preferences in $HOME/.appname or $HOME/.appname/some/subpath.
2. Other data either in the cwd or in $HOME/something.
There are no APIs for locating these places, because the conventions
are so simple you don't actually need one.
Under Mac OS, which is technically a Unix but doesn't act like one,
things are a little more complicated:
1. Preferences stored in the preferences system, which is ultimately
backed by .plist files (specifically) in specific locations, searched
in a particular order. There is an API for setting and getting the
preferences for a given "domain" (read "application"). User-specific
preferences go in
~/Library/Preferences/com.example.your.domain.here.plist, should you
need to manipulate them directly. In Apple's JVM, the default
java.util.Preferences provider is aware of this system.
2. "Documents" - that is, freestanding files users directly care about
and manipulate, under $HOME/Documents[0], with the exact subdirectory
and name up to the user via the save dialog.
3. "Application Data" - that is, files the app needs to maintain in
order to run but that aren't intended for normal manipulation by users
- go in an "Application Data" folder, which is ~/Library/Application
Data/, organized by application name (usually).
There's an added complication in that all of thse names are
localizable; however, there are also APIs for looking up the physical
paths for the various special locations. These APIs are part of the OS
X API, not POSIX or any other standard.
Under Windows, things are pretty much as they are under Mac OS, except
that preferences go in the registry, instead of on the filesystem. Once
again, the default java.util.Preferences provider is aware of this and
does the right thing. There are APIs for looking up the documents and
per-user application data directories, which are part of the Windows
API and are relatively incompatible with their Mac equivalents.
The practical upshot is that the only things you can rely on are:
- The user's home director is at ${user.home} but may not be the right
"default" save location.
- The Preferences API knows where to store settings in a
platform-dependent way.
- The current directory is at ${user.dir} but may not be the right
"default" save location either, for GUI apps. It's reliable for
command-line apps.
- The default Swing L&F probably gets the save dialog right if you
don't specify a directory yourself.
Unfortunately, there's no straightforward way to get either the
Application Data directory or the actual default save location for the
platform. While I'd love to see those in Portadir, I don't imagine it
happening. Writing a portable Java GUI app and getting all of the
various directories correct is pretty much impossible.
I apologize for the disjointed nature of this post; I'm TIRED and I've
been answering phones all day.
-o
[0] Actually, it's worse than that. There are ~/Documents, ~/Music,
~/Movies, ~/Pictures, and ~/Downloads, and application authors are
expected to use the correct one. All of these names are localised, to
boot.