繁体   English   中英

Java:如何检查目录是否在其他目录内

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM