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