简体   繁体   English

Java获取应用程序资源目录

[英]Java Get Application Resource Directory

I am building an application which saves images to disk, and then displays the images within the GUI by reading from disk. 我正在构建一个将图像保存到磁盘的应用程序,然后通过从磁盘读取来显示GUI中的图像。

For testing purposes, I am saving all the images to the user's desktop. 出于测试目的,我将所有图像保存到用户的桌面。

How does Java find its directory to save to (dependent on OS)? Java如何找到要保存的目录(取决于操作系统)?

For example on Mac, I'd expect an application to write to ~/Library/Application Support/Application/, equalls on Windows I'd expect something like C://Program Files/Application/. 例如在Mac上,我希望一个应用程序写入〜/ Library / Application Support / Application /,在Windows上等于我想要的东西如C:// Program Files / Application /。

Does Java provide this functionality, or is this something that's hardcoded, or have I completely missed something here? Java是否提供此功能,或者这是硬编码的东西,还是我完全错过了什么?

Thanks in advance everyone. 在此先感谢大家。

There is no "standard" place where apps are supposed to deposit their files. 应用程序应该存放文件的“标准”位置。 This is completely OS dependent, and there's no standard API that could determine this in a platform-independent way. 这完全取决于操作系统,并且没有标准API可以以独立于平台的方式确定这一点。 It even changes depending on the OS version (Win95 vs XP, vs 7). 它甚至会根据操作系统版本而变化(Win95 vs XP,vs 7)。 A Java app doesn't even need to be "installed", in the sense of copying it into /usr/local/bin or C:\\Program Files\\... . 从将应用程序复制到/usr/local/binC:\\Program Files\\...的角度来看,甚至不需要“安装”Java应用C:\\Program Files\\...

You will have to write OS-specific code or ask the user where to put things. 您必须编写特定于操作系统的代码或询问用户放置内容的位置。 Take a look at System.getProperties() and the java.util.prefs classes. 看一下System.getProperties()java.util.prefs类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM