gpt4 book ai didi

java - 在两个文件路径之间建立相对路径

转载 作者:行者123 更新时间:2023-11-30 06:08:01 25 4
gpt4 key购买 nike

我想比较同一个工作区文件夹中的两个路径,并得到它们之间的相对路径:

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

关于java - 在两个文件路径之间建立相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com