5

I am already familiar with creating files and putting them in "user.home". I'm on Mac, so don't know much about PC's folder, but in my library, there is Application Support. Is there a way to place a directory there and also in PC's %appdata%?

Amundeep Singh
  • 194
  • 5
  • 16
  • what is PC? Do you want to get "Application Support" folder on MAC? or get "ApplicationData" folder on Windows? – donnior Jan 09 '12 at 01:39
  • He means "Application Data" folder on Windows which by default is current user's `%APPDATA%`. But, I've never heard of "Application Support" in Windows terminology before. By the way, `%APPDATA%` requires administrator privilege to be able to modify its content. Equivalent Windows folder for `user.home` is `%USERPROFILE%`. To use any Windows environment variable, just use `System.getenv(string);`. For example: `System.getenv("USERPROFILE");` or `System.getProperty("user.home");` – ee. Jan 09 '12 at 01:45
  • Why not simply use (a sub-directory of) `user.home` as the path? That should work on OS X, Windows **&** *nix. – Andrew Thompson Jan 09 '12 at 02:22
  • Yeah, what I meant was Application Data for Windows, but Mac has it's own Application Support. So I guess I'll just check if a user has `"user.home" + "\\Local Settings\\ApplicationData"`(PC), and if not, I'll just place it in `"user.home" + "/Library/Application Support"` (MAC). – Amundeep Singh Jan 09 '12 at 03:05
  • @AmundeepSingh you can use `System.getProperty("os.name")` to detect which type of operating system (OS) you are using first. – donnior Jan 09 '12 at 04:03

2 Answers2

5

The AppData folder on Windows is "{user.home}\Local Settings\ApplicationData";

You can get it using this:

    String dataFolder = System.getProperty("user.home") + "\\Local Settings\\ApplicationData";

or by this, but it's only works on Windows because the env variable 'APPDATA' only available under Windows.

    String dataFolder = System.getenv("APPDATA");  

Fore more information, you can check it out How to get local application data folder in Java?

Community
  • 1
  • 1
donnior
  • 1,055
  • 1
  • 9
  • 12
  • Note that the first approach is language dependent and is affected by [this issue](http://stackoverflow.com/q/2134338/1804173), so the second approach should be much more reliable. – bluenote10 Jun 07 '15 at 11:57
3

Here is the code I use, you can use it if you want:

public FileManager() {

String FileFolder = System.getenv("APPDATA") + "\\" + "Launcher";

System.out.println("Searching for system");

String os = System.getProperty("os.name").toUpperCase();
if (os.contains("WIN")) {
    FileFolder = System.getenv("APPDATA") + "\\" + "Launcher";
    System.out.println("Found windows");
}
if (os.contains("MAC")) {
    FileFolder = System.getProperty("user.home") + "/Library/Application " + "Support"
            + "Launcher";
    System.out.println("Found mac");
}
if (os.contains("NUX")) {
    FileFolder = System.getProperty("user.dir") + ".Launcher";
    System.out.println("Found linux");
}

System.out.println("Searching for resource folder");
File directory = new File(FileFolder);

if (directory.exists()) {
    System.out.println("Found folder");
}

if (directory.exists() == false) {
    directory.mkdir();
    System.out.println("Could not find folder so created it");
}

}

Only tested on windows can someone test it on mac/Linux?

MCrafterzz
  • 51
  • 1
  • 7