2016-02-14 29 views
1

我很抱歉,如果這個標題嚴重命名的地方,我不能想到那句它一個更好的方式,因此編輯將受到歡迎。需要硬盤的文件存儲我見過查找專用應用程序文件夾

大多數應用程序在不同的操作系統上一個合適的地方創建一個文件夾。在Windows上,這些文件夾位於\ Users \ [當前用戶] \ AppData \ [etc],在Mac上這些文件夾位於/ Users/[當前用戶]/Library/Application Support/[etc]中,Ubuntu有類似的東西,現在想不到。

我想知道,如何在這些文件路徑一直在不同的操作系統與不同的用戶發現,並且是存在的,至少在Java中,一個簡單的方法來實現這一目標?

謝謝。

回答

3

應該有,但是沒有。我甚至提交了一個關於它的bug/RFE,但據我所知,它從未被接受。下面是我用:

public class ApplicationDirectories { 
    private static final Logger logger = 
     Logger.getLogger(ApplicationDirectories.class.getName()); 

    private static final Path config; 

    private static final Path data; 

    private static final Path cache; 

    static { 
     String os = System.getProperty("os.name"); 
     String home = System.getProperty("user.home"); 

     if (os.contains("Mac")) { 
      config = Paths.get(home, "Library", "Application Support"); 
      data = config; 
      cache = config; 
     } else if (os.contains("Windows")) { 
      String version = System.getProperty("os.version"); 
      if (version.startsWith("5.")) { 
       config = getFromEnv("APPDATA", false, 
        Paths.get(home, "Application Data")); 
       data = config; 
       cache = Paths.get(home, "Local Settings", "Application Data"); 
      } else { 
       config = getFromEnv("APPDATA", false, 
        Paths.get(home, "AppData", "Roaming")); 
       data = config; 
       cache = getFromEnv("LOCALAPPDATA", false, 
        Paths.get(home, "AppData", "Local")); 
      } 
     } else { 
      config = getFromEnv("XDG_CONFIG_HOME", true, 
       Paths.get(home, ".config")); 
      data = getFromEnv("XDG_DATA_HOME", true, 
       Paths.get(home, ".local", "share")); 
      cache = getFromEnv("XDG_CACHE_HOME", true, 
       Paths.get(home, ".cache")); 
     } 
    } 

    /** Prevents instantiation. */ 
    private ApplicationDirectories() { 
    } 

    /** 
    * Retrieves a path from an environment variable, substituting a default 
    * if the value is absent or invalid. 
    * 
    * @param envVar name of environment variable to read 
    * @param mustBeAbsolute whether enviroment variable's value should be 
    *      considered invalid if it's not an absolute path 
    * @param defaultPath default to use if environment variable is absent 
    *     or invalid 
    * 
    * @return environment variable's value as a {@code Path}, 
    *   or {@code defaultPath} 
    */ 
    private static Path getFromEnv(String envVar, 
            boolean mustBeAbsolute, 
            Path defaultPath) { 
     Path dir; 
     String envDir = System.getenv(envVar); 
     if (envDir == null || envDir.isEmpty()) { 
      dir = defaultPath; 
      logger.log(Level.CONFIG, 
       envVar + " not defined in environment" 
       + ", falling back on \"{0}\"", dir); 
     } else { 
      dir = Paths.get(envDir); 
      if (mustBeAbsolute && !dir.isAbsolute()) { 
       dir = defaultPath; 
       logger.log(Level.CONFIG, 
        envVar + " is not an absolute path" 
        + ", falling back on \"{0}\"", dir); 
      } 
     } 
     return dir; 
    } 

    /** 
    * Returns directory where the native system expects an application 
    * to store configuration files for the current user. No attempt is made 
    * to create the directory, and no checks are done to see if it exists. 
    * 
    * @param appName name of application 
    */ 
    public static Path configDir(String appName) 
    { 
     return config.resolve(appName); 
    } 

    /** 
    * Returns directory where the native system expects an application 
    * to store implicit data files for the current user. No attempt is made 
    * to create the directory, and no checks are done to see if it exists. 
    * 
    * @param appName name of application 
    */ 
    public static Path dataDir(String appName) 
    { 
     return data.resolve(appName); 
    } 

    /** 
    * Returns directory where the native system expects an application 
    * to store cached data for the current user. No attempt is made 
    * to create the directory, and no checks are done to see if it exists. 
    * 
    * @param appName name of application 
    */ 
    public static Path cacheDir(String appName) 
    { 
     return cache.resolve(appName); 
    } 
} 

一些注意事項:

我不知道對較老版本Windows的代碼甚至是必要的,因爲Java 8不能在Windows XP上運行。

The XDG Directory Specification說:「在這些環境變量中設置的所有路徑必須是絕對的。如果實現遇到任何這些變量中的相對路徑,則應該將該路徑視爲無效並忽略它。「

0

目錄可以用該方法

static String defaultDirectory() { 
    String os = System.getProperty("os.name").toLowerCase(); 
    if (OS.contains("win")) 
     return System.getenv("APPDATA"); 
    else if (OS.contains("mac")) 
     return System.getProperty("user.home") + "/Library/Application Support"; 
    else if (OS.contains("nux")) 
     return System.getProperty("user.home"); 
    else 
     return System.getProperty("user.dir"); 
} 

值得注意的是,在Linux上的任何這樣的文件夾應該從用戶CodeBunnyDenis Tulskiy發現.

(答案開始他們的名字被隱藏找到在this post