Creating a directory in application support or% appdata%

I am already familiar with creating files and putting them in "user.home". I am on a Mac, so I don’t know much about the folder from the PC, but my library has application support. Is there a way to place the directory there, as well as in the% appdata% PC?

+4
source share
2 answers

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

You can get it using this:

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

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

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

For more information, you can check How to get a local Java application data folder?

+4
source

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"); } 

}

Check only on Windows, can anyone check it on mac / Linux?

0
source

Source: https://habr.com/ru/post/1389973/


All Articles