替换Java路径的第一部分
在Java中,我正在寻找一种解决方案,用另一个路径替换路径的第一部分 我发现的唯一解决方案有点难看,您可以在下面的方法替换Java路径的第一部分,java,Java,在Java中,我正在寻找一种解决方案,用另一个路径替换路径的第一部分 我发现的唯一解决方案有点难看,您可以在下面的方法子路径中找到它: import java.nio.file.Path; import java.nio.file.Paths; public class TestPath { static Path subPath(Path newPrefix, Path pathWithOldPrefix){ ; Path result; if(pa
子路径中找到它:
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestPath {
static Path subPath(Path newPrefix, Path pathWithOldPrefix){ ;
Path result;
if(pathWithOldPrefix.getNameCount() > 1) {
result = newPrefix.resolve(pathWithOldPrefix.subpath(1, pathWithOldPrefix.getNameCount()));
}else{
result = newPrefix;
}
return result;
}
public static void main(String[] args) {
//display "/newPrefix/myPath"
System.out.println(subPath(Paths.get("/newPrefix"), Paths.get("/oldPrefix/myPath")));
//display "/newPrefix
System.out.println(subPath(Paths.get("/newPrefix"), Paths.get("/oldPrefix/")));
}
}
有没有更好的解决方案
Path pathWithOldPrefix = Paths.get("/oldPrefix/myPath");
Path oldPrefix = Paths.get("/oldPrefix"); // Or maybe
Path oldPrefix = pathWithOldPrefix.subpath(0, 1);
Path newPrefix = Paths.get("/newPrefix");
Path relative = oldPrefix.relativize(pathWithOldPrefix); // myPath
Path newPath = newPrefix.resolve(relative); // /newPrefix/myPath
Path newPath2 = newPrefix.resolve(oldPrefix.relativize(pathWithOldPrefix));
// /newPrefix/myPath
因此relativize
删除旧前缀(注意参数的顺序)。
而resolve
则添加前缀
因此relativize
删除旧前缀(注意参数的顺序)。
而且resolve
添加了then前缀。感谢@Joop Eggen帮助我找到了正确的解决方案,它并不完美,但它更好地使用了路径:
public class TestPath {
static Path subPath(Path newPrefix, Path pathWithOldPrefix){ ;
Path oldPrefix = pathWithOldPrefix.getRoot().resolve(pathWithOldPrefix.subpath(0, 1));
Path relative = oldPrefix.relativize(pathWithOldPrefix);
return newPrefix.resolve(relative);
}
public static void main(String[] args) {
//display "/newPrefix/myPath"
System.out.println(subPath(Paths.get("/newPrefix"), Paths.get("/oldPrefix/myPath")));
//display "/newPrefix
System.out.println(subPath(Paths.get("/newPrefix"), Paths.get("/oldPrefix/")));
}
}
感谢@Joop Eggen帮助我找到正确的解决方案,它并不完美,但它更好地使用了路径:
public class TestPath {
static Path subPath(Path newPrefix, Path pathWithOldPrefix){ ;
Path oldPrefix = pathWithOldPrefix.getRoot().resolve(pathWithOldPrefix.subpath(0, 1));
Path relative = oldPrefix.relativize(pathWithOldPrefix);
return newPrefix.resolve(relative);
}
public static void main(String[] args) {
//display "/newPrefix/myPath"
System.out.println(subPath(Paths.get("/newPrefix"), Paths.get("/oldPrefix/myPath")));
//display "/newPrefix
System.out.println(subPath(Paths.get("/newPrefix"), Paths.get("/oldPrefix/")));
}
}
路径。相对化
。只需查看路径的整个javadoc,您能详细介绍一下吗?我想在“/newPrefix/myPath”中转换“/oldPrefix/myPath”,我看不出这个方法对我有什么帮助。Path.relative
。只需查看路径的整个javadoc,您能详细介绍一下吗?我想在“/newPrefix/myPath”中转换“/oldPrefix/myPath”,我看不出此方法如何帮助我。您好,我无法接受您的回答,因为使用pathWithOldPrefix.subpath(0,1)的解决方案不起作用。我遇到了异常“'other'是不同类型的路径”。我找到的唯一解决方案是使用:Path oldPrefix=Path.get(“/”+pathWithOldPrefix.subpath(0,1))@杰罗姆没问题,不需要积分。对不起,它没用。让我们继续前进,因为你自己找到了解决方案。请回答您自己的问题,这样就不会有松散的答案无法回答。您好,我无法接受您的答案,因为带有pathWithOldPrefix.subpath(0,1)的解决方案不起作用。我遇到了异常“'other'是不同类型的路径”。我找到的唯一解决方案是使用:Path oldPrefix=Path.get(“/”+pathWithOldPrefix.subpath(0,1))@杰罗姆没问题,不需要积分。对不起,它没用。让我们继续前进,因为你自己找到了解决方案。请回答你自己的问题,这样就不会有不明确的答案。