【问题标题】:How to combine paths in Java?如何在 Java 中组合路径?
【发布时间】:2010-09-29 13:39:06
【问题描述】:

在 C#/.NET 中是否有与 System.IO.Path.Combine() 等效的 Java?或者有什么代码可以做到这一点?

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

【问题讨论】:

  • This SO 问题可能会有所帮助。

标签: java path


【解决方案1】:

假设所有给定路径都是绝对路径。您可以按照下面的 sn-ps 合并这些路径。

String baseURL = "\\\\host\\testdir\\";
String absoluteFilePath = "\\\\host\\testdir\\Test.txt";;
String mergedPath = Paths.get(baseURL, absoluteFilePath.replaceAll(Matcher.quoteReplacement(baseURL), "")).toString();

输出路径为 \\host\testdir\Test.txt。

【讨论】:

    【解决方案2】:

    也许派对迟到了,但我想分享我对此的看法。我正在使用 Builder 模式并允许方便地链接 append(more) 调用,并允许混合 FileString。它可以很容易地扩展以支持使用Path 对象,此外还可以在 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();
    

    生成的absolute 将包含以下内容:

    /hello/world/warez.lha
    

    甚至可能:

    A:\hello\world\warez.lha
    

    【讨论】:

      【解决方案3】:

      此解决方案提供了一个接口,用于连接来自 String[] 数组的路径片段。它使用 java.io.File.File(String parent, String child):

          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"
      
      

      【讨论】:

        【解决方案4】:

        这也适用于 Java 8:

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

        【讨论】:

          【解决方案5】:

          在 Java 7 中,您应该使用 resolve:

          Path newPath = path.resolve(childPath);
          

          虽然 NIO2 Path 类对于具有不必要不同 API 的 File 来说似乎有点多余,但实际上它更加优雅和健壮。

          请注意,Paths.get()(根据其他人的建议)没有采用 Path 的重载,并且执行 Paths.get(path.toString(), childPath)resolve() 不同。来自Paths.get() docs

          请注意,虽然此方法非常方便,但使用它意味着对默认文件系统的假定引用并限制了调用代码的实用性。因此,不应在旨在灵活重用的库代码中使用它。更灵活的替代方法是使用现有的 Path 实例作为锚点,例如:

          Path dir = ...
          Path path = dir.resolve("file");
          

          resolve的姊妹功能是优秀的relativize

          Path childPath = path.relativize(newPath);
          

          【讨论】:

            【解决方案6】:

            平台无关的方法(使用 File.separator,即取决于运行代码的操作系统:

            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
            

            【讨论】:

              【解决方案7】:

              如果不需要多个字符串,可以使用com.google.common.io.Files

              Files.simplifyPath("some/prefix/with//extra///slashes" + "file//name")
              

              得到

              "some/prefix/with/extra/slashes/file/name"
              

              【讨论】:

                【解决方案8】:

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

                【讨论】:

                【解决方案9】:

                您应该使用旨在表示文件系统路径的类,而不是让所有内容都基于字符串。

                如果您使用的是 Java 7 或 Java 8,您应该强烈考虑使用java.nio.file.PathPath.resolve 可用于将一条路径与另一条路径或字符串组合。 Paths 助手类也很有用。例如:

                Path path = Paths.get("foo", "bar", "baz.txt");
                

                如果您需要迎合 Java-7 之前的环境,可以使用 java.io.File,如下所示:

                File baseDirectory = new File("foo");
                File subDirectory = new File(baseDirectory, "bar");
                File fileInDirectory = new File(subDirectory, "baz.txt");
                

                如果您希望稍后将其作为字符串返回,您可以调用getPath()。事实上,如果你真的想模仿Path.Combine,你可以写这样的:

                public static String combine(String path1, String path2)
                {
                    File file1 = new File(path1);
                    File file2 = new File(file1, path2);
                    return file2.getPath();
                }
                

                【讨论】:

                • 当心绝对路径。如果path2 是绝对路径,.NET 版本将返回path2(忽略path1)。 Java 版本会去掉前导的/\ 并将其视为相对路径。
                • @Hugo:所以它浪费了整个两个对象?震惊!老实说,对我来说看起来很干净......它在 File 类中保留了它所属的相对文件名的 逻辑
                • 不过,我认为构造函数new File(String... pathElements) 会更干净,可以添加new File(File basepath, String... pathElements)
                • @modosansreves:看看File.getCanonicalPath
                • @SargeBorsch:C# 只是一种语言。如果您愿意,您可以轻松地在 C# 中创建自己的 File 等价物。 (我假设您的意思是 File 的存在是一种好处,我同意。)
                【解决方案10】:

                这是一个处理多个路径部分和边缘条件的解决方案:

                public static String combinePaths(String ... paths)
                {
                  if ( paths.length == 0)
                  {
                    return "";
                  }
                
                  File combined = new File(paths[0]);
                
                  int i = 1;
                  while ( i < paths.length)
                  {
                    combined = new File(combined, paths[i]);
                    ++i;
                  }
                
                  return combined.getPath();
                }
                

                【讨论】:

                  【解决方案11】:

                  我知道距离 Jon 的原始答案已经很久了,但我对 OP 有类似的要求。

                  通过扩展 Jon 的解决方案,我想出了以下方法,它将采用一个或多个路径段,并采用尽可能多的路径段。

                  用法

                  Path.combine("/Users/beardtwizzle/");
                  Path.combine("/", "Users", "beardtwizzle");
                  Path.combine(new String[] { "/", "Users", "beardtwizzle", "arrayUsage" });
                  

                  此处为有类似问题的其他人编写代码

                  public class Path {
                      public static String combine(String... paths)
                      {
                          File file = new File(paths[0]);
                  
                          for (int i = 1; i < paths.length ; i++) {
                              file = new File(file, paths[i]);
                          }
                  
                          return file.getPath();
                      }
                  }
                  

                  【讨论】:

                    【解决方案12】:

                    为了增强 JodaStephen 的回答,Apache Commons IO 具有执行此操作的 FilenameUtils。示例(在 Linux 上):

                    assert org.apache.commons.io.FilenameUtils.concat("/home/bob", "work\\stuff.log") == "/home/bob/work/stuff.log"
                    

                    它独立于平台,可以生成系统需要的任何分隔符。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2015-04-16
                      • 1970-01-01
                      • 2013-11-18
                      • 1970-01-01
                      • 2015-09-07
                      • 2021-02-10
                      • 1970-01-01
                      相关资源
                      最近更新 更多