查找专用应用文件夹的位置 [英] Find place for dedicated application folder

查看:126
本文介绍了查找专用应用文件夹的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个名称被命名得很糟糕,那么我不能想到一个更好的方法来表达它,所以编辑将被欢迎。



大多数应用程序我看到,需要硬盘驱动器文件存储根据操作系统在合适的位置创建一个文件夹。在Windows上,这些文件夹位于\Users\ [当前用户] \AppData\ [etc]中,在Mac上这些文件夹位于/ Users / [当前用户] /库/应用程序支持/ [etc]中,Ubuntu具有我想知道,这些文件路径在不同用户的不同操作系统中是如何一致地发现的,而且是那么,至少在java中,一个简单的方式来实现呢?



谢谢。

解决方案应该有,但是没有。我甚至提交了一个bug / RFE,但据我所知,这是从来没有被接受的。这是我使用的:

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

private static final Path config;

private static final路径数据;

private static final路径缓存;

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

/ **防止实例化。 * /
private ApplicationDirectories(){
}

/ **
*从环境变量中检索路径,替换默认的
*价值不存在或无效。
*
* @param envVar要读取的环境变量的名称
* @param mustBeAbsolute环境变量的值是否应为
*如果不是绝对路径,则认为无效
* @param defaultPath默认使用环境变量缺少
*或无效
*
* @return环境变量的值作为{@code Path},
*或{@代码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 +没有在环境中定义
+,回落在\{0} \,dir);
} else {
dir = Paths.get(envDir);
if(mustBeAbsolute&!dir.isAbsolute()){
dir = defaultPath;
logger.log(Level.CONFIG,
envVar +不是绝对路径
+,落在\{0} \,dir);
}
}
return dir;
}

/ **
*返回本地系统期望应用程序
*存储当前用户的配置文件的目录。没有尝试
*来创建目录,并且没有检查是否存在。
*
* @param appName应用程序名称
* /
public static Path configDir(String appName)
{
return config.resolve(appName) ;
}

/ **
*返回本地系统期望应用程序
*存储当前用户的隐式数据文件的目录。没有尝试
*来创建目录,并且没有检查是否存在。
*
* @param appName应用程序名称
* /
public static Path dataDir(String appName)
{
return data.resolve(appName) ;
}

/ **
*返回本地系统期望应用程序
*存储当前用户的缓存数据的目录。没有尝试
*来创建目录,并且没有检查是否存在。
*
* @param appName应用程序名称
* /
public static Path cacheDir(String appName)
{
return cache.resolve(appName) ;
}
}

一些注释:



我不知道旧版本的代码甚至是必要的,因为Java 8不能在Windows XP上运行。



XDG目录规范说:这些环境变量中设置的所有路径都必须绝对。如果某个实现在任何这些变量中遇到相对路径,则应该将该路径无效并忽略它。


I'm sorry if this title is badly named, I can't think of a better way to phrase it and so edits would be welcomed.

Most applications I've seen that require hard drive file storage create a folder in a suitable place depending on the operating system. On Windows these folders lie in \Users\[current user]\AppData\[etc], on Mac these folders lie in /Users/[current user]/Library/Application Support/[etc], Ubuntu has a similar thing that I can't think of right now.

I would like to know, how are these file paths consistently found in differing operating systems with different users, and is there, at least in java, a simple way to achieve this?

Thank you.

解决方案

There should be, but there isn't. I even submitted a bug/RFE about it, but as far as I know, it was never accepted. Here's what I use:

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

Some notes:

I'm not sure the code for older Windows versions is even necessary, as Java 8 doesn't run on Windows XP.

The XDG Directory Specification says "All paths set in these environment variables must be absolute. If an implementation encounters a relative path in any of these variables it should consider the path invalid and ignore it."

这篇关于查找专用应用文件夹的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆