How to handle ~ in file paths

2021/01/17 00:41 · java ·  · 0评论

I'm writing a simple command line Java utility. I would like the user to be able to pass in a file path relative to their home directory using the ~ operator. So something like ~/Documents/...

My question is is there a way to make Java resolve this type of path automatically? Or do I need to scan the file path for the ~ operator?

It seems like this type of functionality should be baked into the File object. But it doesn't seem to be.

A simple path = path.replaceFirst("^~", System.getProperty("user.home")); when it is gotten from the user (before making a File out of it) should be enough to work in most cases - because the tilde is only expanded to a home directory if it is the first character in a directory section of a path.

This is shell-specific expansion, so you need to replace it at the beginning of the line, if present:

String path = "~/xyz";
...
if (path.startsWith("~" + File.separator)) {
    path = System.getProperty("user.home") + path.substring(1);
} else if (path.startsWith("~")) {
    // here you can implement reading homedir of other users if you care
    throw new UnsupportedOperationException("Home dir expansion not implemented for explicit usernames");
}

File f = new File(path);
...

As Edwin Buck pointed out in the comment to another answer, ~otheruser/Documents should also expand correctly. Here's a function that worked for me:

public String expandPath(String path) {
    try {
        String command = "ls -d " + path;
        Process shellExec = Runtime.getRuntime().exec(
            new String[]{"bash", "-c", command});

        BufferedReader reader = new BufferedReader(
            new InputStreamReader(shellExec.getInputStream()));
        String expandedPath = reader.readLine();

        // Only return a new value if expansion worked.
        // We're reading from stdin. If there was a problem, it was written
        // to stderr and our result will be null.
        if (expandedPath != null) {
            path = expandedPath;
        }
    } catch (java.io.IOException ex) {
        // Just consider it unexpandable and return original path.
    }

    return path;
}

A fairly streamlined answer that works with paths with actual ~ characters in them:

String path = "~/Documents";
path.replaceFirst("^~", System.getProperty("user.home"));

Previously mentioned solutions do not behave as expected when user home contains '\' or other special chars. This works for me:

path = path.replaceFirst("^~", Matcher.quoteReplacement(System.getProperty("user.home")));
本文地址:http://java.askforanswer.com/how-to-handle-in-file-paths.html
文章标签:
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!