问题
I cant seem to find a solution.
I have two paths, like:
D:/Views/me/a.b
AND
D:/Views/me/a
or
D:/Views/me/a.b
AND
D:/Views/me/a.b/x/y
I have to ensure, that one file/directory is not a child of the other.
I have tried Contains
but it does not work for me in this case?
回答1:
Try using startsWith api of String:
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
回答2:
I think Path and Paths from java.nio.file can be useful here (if you have at least 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);
}
You might consider using toAbsolutePath() or toRealPath() on the paths before the check depending on your needs.
Here's the the official Java tutorial for Path Operations.
回答3:
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");
}
回答4:
Surprisingly there is no simple, yet functional solution.
Here is one using java.nio.file.Path API only:
static boolean isChildPath(Path parent, Path child){
Path pn = parent.normalize();
Path cn = child.normalize();
return cn.getNameCount() > pn.getNameCount() && cn.startsWith(pn);
}
Test cases:
@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();
}
来源:https://stackoverflow.com/questions/28698125/java-check-if-path-is-parent-of-a-file