开发者

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

While I was working on my project I received a requirement: I开发者_StackOverflow 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.


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜