[英]Java: How to check if a directory is inside other directory
How can I check if a given file is under other file? 如何检查给定文件是否在其他文件下?
Ie I have new Paths.get("/foo/bar/")
and new Paths.get("./abc/def.jar")
. 即我有
new Paths.get("/foo/bar/")
和new Paths.get("./abc/def.jar")
。 And I want to check whether the second is under /foo/bar
. 我想检查第二个是否在
/foo/bar
。
I can figure out some string based comparisons like path.toFile().getAbsolutePath().startsWith(path2.toFile().getAbsolutePath())
, but that looks fragile and not right. 我可以找出一些基于字符串的比较,例如
path.toFile().getAbsolutePath().startsWith(path2.toFile().getAbsolutePath())
,但这看起来很脆弱,并不正确。
我发现了这个有用的Path.startsWith( Path other )
方法,因此对我有用:
inputPath.toAbsolutePath().startsWith(outputDirectory.toAbsolutePath())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.