2

I'm looking for a platform-independent way of getting a location where my Java program can save files. This location should be user-dependent.

What I want is:

  • %userprofile%\AppData\Local\MyAppName\ on Windows,
  • ~/.local/share/MyAppName/ or something like that on Ubuntu
  • Anything else corresponting so it works on every platform
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
s3lph
  • 4,575
  • 4
  • 21
  • 38
  • Why don't you pick any location inside your application directory? – Juned Ahsan Jan 03 '14 at 16:07
  • And what would my application directory be? How do I get it? – s3lph Jan 03 '14 at 16:10
  • When a java class runs it by default creates a file at the same path where the class file is located. – Juned Ahsan Jan 03 '14 at 16:12
  • And when running from a JAR? – s3lph Jan 03 '14 at 16:14
  • @JunedAhsan On most newer versions of Windows (> Vista, I think), the user doesn't have write privileges to the program's folder. (In a lot of setups) – aryn.galadar Jan 03 '14 at 16:15
  • see http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java and http://stackoverflow.com/questions/1198911/how-to-get-local-application-data-folder-in-java – Mark W Jan 03 '14 at 16:17
  • 2
    I tend to use a subdirectory of the user home folder myself. The most successful and flexible solution of all: make your application ask the user where to put stuff. – Gimby Jan 03 '14 at 16:20
  • @Gimby I think I'll do that... – s3lph Jan 03 '14 at 16:27

0 Answers0