项目新会话的 Java 临时文件
我正在开发的项目需要保存项目的设置,当程序启动时,它应该从设置中读取文件。 在 Windows 中,我无权在我想要的任何地方创建文件。我不想在临时文件夹中创建,因为它可以在一段时间后被删除。知道我应该把文件放在哪里吗?
I am working on project which needs setting of the project to be saved and when the program starts it should read the file from the settings.
In windows I dont have permission to make the file where-ever I want. I dont want to make in the temp folder as it can be deleted after some time. Any idea where should i place the file ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以查看以下内容:
http://www.mindspring.com /~mgrand/java-system-properties.htm
其中,我看到以下内容被使用:
用于临时文件。但是,我认为 user.dir 使用起来最安全,而 user.home 是最常见的。
编辑:似乎常见的做法是在启动和重新启动时将 user.home 用于应用程序将重用的文件,而 user.dir 更常用于临时文件。
所以,我建议使用:
因为你的java将有在那里读写的权限,所以你永远不会出错。
You can take a look at the following:
http://www.mindspring.com/~mgrand/java-system-properties.htm
Of these, I have seen the following being used:
being used for temporary files. However, I would assume that user.dir would be safest to use, while user.home would be the most commonplace.
EDIT: It seems the common practice is to use user.home for files that will be reused by your app, when starting and restarting, while user.dir is more commonly used for temporary files.
So, I reccommend using:
As your java will have the permissions to read and write there, so you can never go wrong.
如果这是某种用户偏好,请使用 System.getEnv("APP_DATA")
如果 System.getEnv("LOCA_APP_DATA") 也与本地系统(即运行它的计算机)相关,请使用 System.getEnv("LOCA_APP_DATA")。
请记住,这不适用于 Windows 以外的其他平台。
要真正跨平台,您可以使用 System.getProperty("user.home") ,它指向用户主目录。
Use System.getEnv("APP_DATA") if this some kind of user preference
Use System.getEnv("LOCA_APP_DATA") if it also related to the local system (ie, the computer it is running on).
Remember that this does not work on other platforms than Windows.
To have something really cross-platform, you can use System.getProperty("user.home") which points to the user home directory.
如果您只需要存储首选项,则根本不需要文件,应该使用 首选项 API。无需担心权限、交叉兼容性等问题。
If you only need to store preferences, you don't need a file at all and should use the Preferences API. No need to worry about permissions, cross-compatibility, etc.