0

To access the Appdata folder in Windows, I can just use %APPDATA% in the path name and that's it.

Does this method work on Linux and other OS as well? I know there alternatives to using %APPDATA%, but my question is just if I can use it there, and how the path name is translated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
AyCe
  • 727
  • 2
  • 11
  • 30
  • There's no direct equivalent to AppData, no, unless you count the home directory - typically you'd just store your settings in a ".whatever" child path of the home directory. Linux has no equivalent of Window's local / roaming / locallow split either. – Rup Jul 07 '13 at 22:51
  • This works fine for me: `public static String getAppdataDir() { String os = System.getProperty("os.name"); if (os.toLowerCase().contains("windows")) { return System.getenv("APPDATA"); } else { return System.getProperty("user.home"); } }` – AyCe Jul 08 '13 at 00:02
  • What type of app. is it? E.G. desktop app., applet, web-app. .. – Andrew Thompson Jul 08 '13 at 05:31
  • Hmm, note then that the conventions what you put in that directory are different on the different OSes though - you want more than that really. – Rup Jul 08 '13 at 09:19
  • It's a desktop app. Also, I got methods for using ".dirname" on Linux and Other and "Dirname" on Windows, so it should be alright. Don't know about Mac. These directories are for settings and profiles. – AyCe Jul 10 '13 at 20:41

1 Answers1

2

%APPDATA% is a windows only shortcut. The Mac equivalent is usually ~/Library/Application Support, but user specific information is in ~\Library\Preferences and the linux equivalent depends on the specific application. For instance, Chrome data is usually in ~\.Chrome for linux.

Epicblood
  • 1,167
  • 2
  • 10
  • 29
  • And what would happen if I'd have code creating a file "%APPDATA%/Subfolder/file.txt" with this exact path under Linux? – AyCe Jul 07 '13 at 23:02
  • 1
    It would most likely throw a `FileNotFoundException` – Epicblood Jul 07 '13 at 23:09
  • Wrap it with an OS check so that it only executes if it's on windows, I use `String osName = System.getProperty("os.name");` and then check if osName contains `Windows` by doing something like `if(osName.toLowerCase().contains("windows"){ //blablabla}` – Epicblood Jul 07 '13 at 23:11
  • Okay, so it seems I finally have to include this wrapper thing. Thanks :) – AyCe Jul 07 '13 at 23:16