【问题标题】:Find place for dedicated application folder查找专用应用程序文件夹的位置
【发布时间】:2016-02-14 06:02:02
【问题描述】:

很抱歉,如果这个标题命名不当,我想不出更好的表达方式,因此欢迎编辑。

我见过的大多数需要硬盘文件存储的应用程序都会根据操作系统在合适的位置创建一个文件夹。在 Windows 上,这些文件夹位于 \Users\[current user]\AppData\[etc],在 Mac 上,这些文件夹位于 /Users/[current user]/Library/Application Support/[etc],Ubuntu 有一个类似的东西,我现在想不出来。

我想知道,这些文件路径如何在具有不同用户的不同操作系统中始终如一地找到,至少在 java 中是否有一种简单的方法来实现这一点?

谢谢。

【问题讨论】:

    标签: java file


    【解决方案1】:

    应该有,但没有。我什至提交了一个关于它的错误/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 说“在这些环境变量中设置的所有路径都必须是绝对的。如果实现在任何这些变量中遇到相对路径,它应该认为路径无效并忽略它。”

    【讨论】:

    • 发布您提交的错误/问题的链接?
    • @Ryan 它从未被接受,所以据我所知没有错误或 RFE。
    【解决方案2】:

    通过方法可以找到目录

    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 上,任何此类文件夹都应通过以 . 开头的名称来隐藏

    (从this post 上的用户CodeBunnyDenis Tulskiy 找到答案)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 2020-05-05
      相关资源
      最近更新 更多