How can I get system user documents, pictures, music folders independent of OS using Java?

半城伤御伤魂 提交于 2019-12-08 03:09:45

问题


While I was working on my project I received a requirement: I need to get the system user documents folder (like “My Documents” for Windows and “Documents” for Linux and Mac OS X). I need to get it for all the operating systems. How can I do this?

I can get the user root directory by using System.getProperty("user.home"). I need a solution for this requirement using Java.


回答1:


The location of the default system "Documents" or "Pictures" folder is highly operating-system dependent, distribution dependent, user setting dependent. The closest thing I can come up with is to start looking in the user home directory which you can get from System.getProperty("user.home").

You could then implement some heuristics based on

  • System.getProperty("os.name") Operating system name
  • System.getProperty("os.arch") Operating system architecture (should be less interesting)
  • System.getProperty("os.version") Operating system version



回答2:


I believe Java currently does not provide such functionality, so you would have to implement your own abstraction that could use system property to detect the runtime platform and select an appropriate factory for the current platform. Then factory would return platform-specific folders.




回答3:


Not only does this vary from operating system to operating system, but it also varies from user to user. My suggestion would be to create a prioritized list of common names for these locations, and then to search through the list, returning the first directory in the list that exists. If none of the elements in the list exist, then you would report that such directory does not exist or give some other type of error message. It might also be nice to provide a configuration file or some other means by which the default search could be overridden for situations where the default list turned out to be insufficient.




回答4:


The existing answers are not wrong, but are lacking quite some detail. There are more complete answers on SO, but none answer the question in general for the most relevant operating systems.

  • Windows: the way to go seems to be using SHGetFolderPath (or an equivalent library call depending on the version of Windows) as mentioned in this answer to How do I get the Users Music Directory?.
  • Linux (and probably Solaris and *Bsd): xdg-user-dir (executable) is the correct tool. Possible integrations are listed in answers to Using XDG directory specification on Java application (java-gnome, xdg-java).
  • Mac OS: only Unix-like $HOME (= user.home system property) seems to be provided.

As another reference point: .Net Core puts quite some effort into mapping equivalent special folders between operating systems: https://github.com/dotnet/corefx/blob/master/src/System.Runtime.Extensions/src/System/Environment.Unix.cs (GetFolderPathCoreWithoutValidation).

John Koerner compares the actual locations of these special folders between Windows and Mac OS X.



来源:https://stackoverflow.com/questions/2896610/how-can-i-get-system-user-documents-pictures-music-folders-independent-of-os-u

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