Java: 检查路径是否为文件的 parent
Java: Check if path is parent of a file
我似乎找不到解决办法。
我有两条路,比如:
D:/Views/me/a.b
和
D:/Views/me/a
或
D:/Views/me/a.b
和
D:/Views/me/a.b/x/y
我必须确保一个 file/directory 不是另一个的 child。
我试过 Contains
但在这种情况下它对我不起作用?
尝试使用字符串的 startsWith api:
String str1 = "D:/Views/me/a.b";
String str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
System.out.println("Yes it is");
}
str1 = "D:/Views/me/a/c/d";
str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
System.out.println("Yes it is");
}
Output:
Yes it is
Yes it is
String path = "D:/Views/me/a.b";
String path2 = "D:/Views/me/a";
File file = new File(path);
if(file.getParentFile().getAbsolutePath().equals("path2")){
System.out.println("file is parent");
}else{
System.out.println("file is not parent");
}
我认为 java.nio.file 中的 Path and Paths 在这里很有用(如果你至少有 Java 7)。
public static void main(String[] args) {
Path p1 = Paths.get("D:/Views/me/a.b");
Path p2 = Paths.get("D:/Views/me/a");
System.out.println(isChild(p1, p2));
System.out.println(isChild(p2, p1));
Path p3 = Paths.get("D:/Views/me/a.b");
Path p4 = Paths.get("D:/Views/me/a.b/x/y");
System.out.println(isChild(p3, p4));
System.out.println(isChild(p4, p3));
}
//Check if childCandidate is child of path
public static boolean isChild(Path path, Path childCandidate) {
return childCandidate.startsWith(path);
}
您可以根据需要考虑在检查前的路径上使用 toAbsolutePath() 或 toRealPath()。
这是 Path Operations 的官方 Java 教程。
令人惊讶的是,没有简单但实用的解决方案。
这是仅使用 java.nio.file.Path API 的一个:
static boolean isChildPath(Path parent, Path child){
Path pn = parent.normalize();
Path cn = child.normalize();
return cn.getNameCount() > pn.getNameCount() && cn.startsWith(pn);
}
测试用例:
@Test
public void testChildPath() {
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F"))).isFalse();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/A"))).isTrue();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/A.txt"))).isTrue();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/../A"))).isFalse();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/FA"))).isFalse();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderA"))).isFalse();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderA/B"))).isTrue();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderA/B"))).isTrue();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderAB"))).isFalse();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/Z/X/../A"))).isTrue();
}
我似乎找不到解决办法。
我有两条路,比如:
D:/Views/me/a.b
和
D:/Views/me/a
或
D:/Views/me/a.b
和
D:/Views/me/a.b/x/y
我必须确保一个 file/directory 不是另一个的 child。
我试过 Contains
但在这种情况下它对我不起作用?
尝试使用字符串的 startsWith api:
String str1 = "D:/Views/me/a.b";
String str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
System.out.println("Yes it is");
}
str1 = "D:/Views/me/a/c/d";
str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
System.out.println("Yes it is");
}
Output:
Yes it is
Yes it is
String path = "D:/Views/me/a.b";
String path2 = "D:/Views/me/a";
File file = new File(path);
if(file.getParentFile().getAbsolutePath().equals("path2")){
System.out.println("file is parent");
}else{
System.out.println("file is not parent");
}
我认为 java.nio.file 中的 Path and Paths 在这里很有用(如果你至少有 Java 7)。
public static void main(String[] args) {
Path p1 = Paths.get("D:/Views/me/a.b");
Path p2 = Paths.get("D:/Views/me/a");
System.out.println(isChild(p1, p2));
System.out.println(isChild(p2, p1));
Path p3 = Paths.get("D:/Views/me/a.b");
Path p4 = Paths.get("D:/Views/me/a.b/x/y");
System.out.println(isChild(p3, p4));
System.out.println(isChild(p4, p3));
}
//Check if childCandidate is child of path
public static boolean isChild(Path path, Path childCandidate) {
return childCandidate.startsWith(path);
}
您可以根据需要考虑在检查前的路径上使用 toAbsolutePath() 或 toRealPath()。
这是 Path Operations 的官方 Java 教程。
令人惊讶的是,没有简单但实用的解决方案。
这是仅使用 java.nio.file.Path API 的一个:
static boolean isChildPath(Path parent, Path child){
Path pn = parent.normalize();
Path cn = child.normalize();
return cn.getNameCount() > pn.getNameCount() && cn.startsWith(pn);
}
测试用例:
@Test
public void testChildPath() {
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F"))).isFalse();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/A"))).isTrue();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/A.txt"))).isTrue();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/../A"))).isFalse();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/FA"))).isFalse();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderA"))).isFalse();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderA/B"))).isTrue();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderA/B"))).isTrue();
assertThat(isChildPath(Paths.get("FolderA"), Paths.get("FolderAB"))).isFalse();
assertThat(isChildPath(Paths.get("/FolderA/FolderB/F"), Paths.get("/FolderA/FolderB/F/Z/X/../A"))).isTrue();
}