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

a 夏天 提交于 2019-12-18 08:51:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!