【问题标题】:Build relative path between two file path在两个文件路径之间建立相对路径
【发布时间】:2017-03-29 14:29:01
【问题描述】:

我想比较同一个工作空间文件夹中的两条路径,并获取它们之间的相对路径:

String firPath = "C:/toto/tata/test1/test2/img/1.jpg" // test example
String secPath = "C:/toto/tata/test1/img/1.jpg" // test example

并从secondPath返回firstPath相对路径

example = "../../img/"

我发现了很多不同语言(python、.net、c++ ...)的示例:

How to get relative path from absolute path

compare path and get relative path between two files with javascript

...但是 java 没有解决方案。

大多数时候,使用的是库方法,我想知道 java 是否有我可以使用的相同方法。

感谢您的帮助。

【问题讨论】:

    标签: java path relative-path


    【解决方案1】:

    在 Java 7 中添加 Path.relativize 怎么样?

    Path first = Paths.get(firstPath); Path second = Paths.get(secondPath);
    System.out.println(first.relativize(second)); 
    System.out.println(second.relativize(first)); 
    

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 2013-06-20
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2013-04-24
      相关资源
      最近更新 更多