如何检查给定文件是否在其他文件下?例如,我有 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())
关于java - 如何检查目录是否在其他目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26518033/