在c# /.NET中有System.IO.Path.Combine()的Java等价程序吗?或者任何代码来完成这个?

这个静态方法将一个或多个字符串组合成一个路径。


当前回答

该解决方案提供了一个接口,用于连接String[]数组中的路径片段。它使用java.io.File。文件(父字符串,子字符串):

    public static joinPaths(String[] fragments) {
        String emptyPath = "";
        return buildPath(emptyPath, fragments);
    }

    private static buildPath(String path, String[] fragments) {
        if (path == null || path.isEmpty()) {
            path = "";
        }

        if (fragments == null || fragments.length == 0) {
            return "";
        }

        int pathCurrentSize = path.split("/").length;
        int fragmentsLen = fragments.length;

        if (pathCurrentSize <= fragmentsLen) {
            String newPath = new File(path, fragments[pathCurrentSize - 1]).toString();
            path = buildPath(newPath, fragments);
        }

        return path;
    }

然后你可以这样做:

String[] fragments = {"dir", "anotherDir/", "/filename.txt"};
String path = joinPaths(fragments);

返回:

"/dir/anotherDir/filename.txt"

其他回答

该解决方案提供了一个接口,用于连接String[]数组中的路径片段。它使用java.io.File。文件(父字符串,子字符串):

    public static joinPaths(String[] fragments) {
        String emptyPath = "";
        return buildPath(emptyPath, fragments);
    }

    private static buildPath(String path, String[] fragments) {
        if (path == null || path.isEmpty()) {
            path = "";
        }

        if (fragments == null || fragments.length == 0) {
            return "";
        }

        int pathCurrentSize = path.split("/").length;
        int fragmentsLen = fragments.length;

        if (pathCurrentSize <= fragmentsLen) {
            String newPath = new File(path, fragments[pathCurrentSize - 1]).toString();
            path = buildPath(newPath, fragments);
        }

        return path;
    }

然后你可以这样做:

String[] fragments = {"dir", "anotherDir/", "/filename.txt"};
String path = joinPaths(fragments);

返回:

"/dir/anotherDir/filename.txt"

也许来晚了,但我想分享一下我对这件事的看法。我不喜欢把整个库都拉来做这样的事情。相反,我使用Builder模式,并允许方便地链式附加(更多)调用。它甚至允许混合文件和字符串,并且可以很容易地扩展到支持路径。此外,它可以在Linux、Macintosh等上自动正确地处理不同的路径分隔符。

public class Files  {
    public static class PathBuilder {
        private File file;

        private PathBuilder ( File root ) {
            file = root;
        }

        private PathBuilder ( String root ) {
            file = new File(root);
        }

        public PathBuilder append ( File more ) {
            file = new File(file, more.getPath()) );
            return this;
        }

        public PathBuilder append ( String more ) {
            file = new File(file, more);
            return this;
        }

        public File buildFile () {
            return file;
        }
    }

    public static PathBuilder buildPath ( File root ) {
        return new PathBuilder(root);
    }

    public static PathBuilder buildPath ( String root ) {
        return new PathBuilder(root);
    }
}

用法示例:

File root = File.listRoots()[0];
String hello = "hello";
String world = "world";
String filename = "warez.lha"; 

File file = Files.buildPath(root).append(hello).append(world)
              .append(filename).buildFile();
String absolute = file.getAbsolutePath();

生成的绝对值将包含如下内容:

/hello/world/warez.lha

或者甚至:

A:\hello\world\warez.lha

平台独立的方法(使用File。分隔符,即将工作取决于代码运行的操作系统:

java.nio.file.Paths.get(".", "path", "to", "file.txt")
// relative unix path: ./path/to/file.txt
// relative windows path: .\path\to\filee.txt

java.nio.file.Paths.get("/", "path", "to", "file.txt")
// absolute unix path: /path/to/filee.txt
// windows network drive path: \\path\to\file.txt

java.nio.file.Paths.get("C:", "path", "to", "file.txt")
// absolute windows path: C:\path\to\file.txt

主要的答案是使用File对象。然而,Commons IO确实有一个类FilenameUtils可以做这种事情,比如concat()方法。

这也适用于Java 8:

Path file = Paths.get("Some path");
file = Paths.get(file + "Some other path");