Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

I've checked the system properties list, but it doesn't seem to include the user's documents folder.

like image 589
Raceimaztion Avatar asked Apr 08 '11 03:04

Raceimaztion


2 Answers

Sadly there is no easy cross-platform way. You will have to take advantage of native functionality on each OS platform

  • Here is some info on how to do it in osx
  • Here is some info on how to do it in 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. The system property user.home should at least be valid in Linux.

like image 122
Justin Waugh Avatar answered Oct 12 '22 23:10

Justin Waugh


Partial solution:

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

// the folder name is the same whatever the language of Mac OSX.

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

/Users/david/Documents

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

C:\Documents and Settings\david\Application Data

like image 33
David Portabella Avatar answered Oct 13 '22 01:10

David Portabella