问题
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?
回答1:
System.getProperty("user.home")
回答2:
As well as user.home
, the following is useful for more temporary file storage:
System.getProperty("java.io.tmpdir")
回答3:
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
回答4:
You can consider storing the data at Roaming directory under Windows.
来源:https://stackoverflow.com/questions/1570757/storing-a-file-in-the-users-directory-in-cross-platform-java