简体   繁体   English

跨平台方式询问用户的文档文件夹?

[英]Cross-platform way to ask for the user's documents folder?

I'm writing a cross-platform program in Java and want to stick the configuration files in the user's documents folder ("My Documents" under Windows, "Documents" under appropriate Linux, and whatever the folder's called under Mac OS), but I'm not sure how to ask Java for that. 我正在用Java编写一个跨平台程序,并希望将配置文件粘贴在用户的文档文件夹中(Windows下的“我的文档”,适当的Linux下的“文档”,以及Mac OS下调用的文件夹),但是我我不知道怎么问Java。

I'd like to stay away from hard-coding things (do X if we're on Windows, Y if we're on Linux, or Z if we're on OS X), as this puts the burden of support on my shoulders rather than the Oracle development team. 我想远离硬编码的东西(如果我们在Windows上做X ,如果我们在Linux上则为Y ,如果我们在OS X上则为Z ),因为这会给我带来支持的负担肩膀而不是Oracle开发团队。

I've checked the system properties list, but it doesn't seem to include the user's documents folder. 我已经检查了系统属性列表,但它似乎没有包含用户的文档文件夹。

Sadly there is no easy cross-platform way. 可悲的是,没有简单的跨平台方式。 You will have to take advantage of native functionality on each OS platform 您必须利用每个OS平台上的本机功能

  • Here is some info on how to do it in osx 以下是有关如何在osx中执行此操作的一些信息
  • Here is some info on how to do it in windows 以下是有关如何在Windows中执行此操作的一些信息

For Linux I don't have a convenient link, but given that there isn't necessarily the concept of a Documents folder in Linux, I don't know of a good solution. 对于Linux我没有方便的链接,但鉴于Linux中不一定有Documents文件夹的概念,我不知道一个好的解决方案。 The system property user.home should at least be valid in Linux. 系统属性user.home至少应该在Linux中有效。

Partial solution: 部分解决方案:

boolean isMac = System.getProperty("os.name").equals("Mac OS X"); boolean isMac = System.getProperty(“os.name”)。equals(“Mac OS X”);

Or use http://commons.apache.org/vfs/ to get the operating system: http://commons.apache.org/vfs/apidocs/org/apache/commons/vfs2/util/Os.html 或者使用http://commons.apache.org/vfs/获取操作系统: http//commons.apache.org/vfs/apidocs/org/apache/commons/vfs2/util/Os.html

// the folder name is the same whatever the language of Mac OSX. //文件夹名称与Mac OSX的语言相同。

Mac: System.getProperty("user.home")+File.separator+"Documents"); Mac:System.getProperty(“user.home”)+ File.separator +“Documents”);

/Users/david/Documents /用户/大卫/文件

Win: System.getenv("APPDATA")); Win:System.getenv(“APPDATA”));

C:\\Documents and Settings\\david\\Application Data C:\\ Documents and Settings \\ david \\ Application Data

Ask the user, somehow, where they want to save data to, or use the current directory (simple relative paths) and provide appropriate instructions for set up. 以某种方式询问用户他们想要将数据保存到何处,或使用当前目录(简单相对路径)并提供适当的设置说明。 One means of "asking" is having a property that can be set via the command line. “询问”的一种方法是具有可以通过命令行设置的属性。

Don't just pollute the user's home directory with your application's stuff - how do you know how they like their document tree organized? 不要只用你的应用程序的东西污染用户的主目录 - 你怎么知道他们如何组织他们的文档树?

I am absolutely fed up with *nix oriented programs dropping their little config files and data folders into the root of my documents folder. 我完全厌倦了面向* nix的程序,将他们的小配置文件和数据文件夹放入我的文档文件夹的根目录。

This is usually not possible in a generic way. 这通常不可能以通用方式实现。 You will have to use platform dependent apstractions. 您将不得不使用平台相关的apstractions。 I do it like this for AppData eg, on Windows I detect the AppData/Roaming or AppData/Local, depending on the data I need to store, on other Platforms I create a folder ".myappname" in the userhome, and use this. 我这样对AppData这样做,例如,在Windows上我检测到AppData / Roaming或AppData / Local,这取决于我需要存储的数据,在其他平台上我在userhome中创建了一个文件夹“.myappname”,并使用它。

For the documents folder, you will have to read the registry. 对于documents文件夹,您必须阅读注册表。 Before Windows Vista using the user.home propery + "/Documents" is not enought, because in other languages it might be "/Dokumente" (german) or something else. 在Windows Vista之前使用user.home propery +“/ Documents”是不合适的,因为在其他语言中它可能是“/ Dokumente”(德语)或其他东西。 Just the registry has the real path to this folder. 只是注册表有这个文件夹的真实路径。

On linux platforms it depends on the Desktop environment. 在Linux平台上,它取决于桌面环境。 You will just have to try it out. 你只需要尝试一下。 Gnome and KDE use different places to store the Documents folder, IMHO. Gnome和KDE使用不同的地方来存储Documents文件夹,恕我直言。 And if you just use FVWM there, there is no predefined place for docs except the user.home property, what is a good fallback. 如果你只是在那里使用FVWM,除了user.home属性之外,没有预定义的文档位置,什么是好的后备。

Assuming this is an application which requires installation, Why not ask the user to specify the location during installation. 假设这是一个需要安装的应用程序,为什么不要求用户在安装过程中指定位置。 Generate a run script using this information which would set the appropriate environment variables. 使用此信息生成运行脚本,该脚本将设置适当的环境变量。 As a norm the user is not expected to run the java command! 作为一种规范,用户不应该运行java命令!

When the user starts the application using this run script, the application can read the environment variables. 当用户使用此运行脚本启动应用程序时,应用程序可以读取环境变量。

Alternatively, create a jar file with configuration files. 或者,使用配置文件创建一个jar文件。 Read the config files using Classloader's getResourceAsStream. 使用Classloader的getResourceAsStream读取配置文件。 As long as the configuration jar is under the classpath you can access the files. 只要配置jar位于类路径下,您就可以访问这些文件。 This also has the added advantage of hiding the configuration from accidental modification. 这还具有隐藏配置以防意外修改的附加优点。 This should work for folders as well (if the config needs to be modified by the user). 这也适用于文件夹(如果配置需要由用户修改)。

What is wrong with hardcoding a way to get the path depending on the users operating system? 根据用户操作系统硬编码获取路径的方法有什么问题? There can be large variances and OS's work differently with different names. 可能存在大的差异,OS的工作方式也不同。 Simply on launch find the path of the documents folder based on their OS and continue to reference that path wherever it is needed. 只需在启动时根据操作系统找到文档文件夹的路径,并继续在任何需要的地方引用该路径。

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

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