检查文件是否在(子)目录中
我想检查现有文件是在特定目录中还是在其子目录中。
我有两个File对象。
File dir; File file;
两者都保证存在。 我们假设
dir = /tmp/dir file = /tmp/dir/subdir1/subdir2/file.txt
我希望此检查返回true
现在我正在以这种方式进行检查:
String canonicalDir = dir.getCanonicalPath() + File.separator; boolean subdir = file.getCanonicalPath().startsWith(canonicalDir);
这似乎适用于我的有限测试,但我不确定这是否会在某些操作系统上出现问题。 我也不喜欢getCanonicalPath()可以抛出我必须处理的IOException。
有没有更好的办法? 可能在某些图书馆?
谢谢
我会创建一个小实用程序方法:
public static boolean isInSubDirectory(File dir, File file) { if (file == null) return false; if (file.equals(dir)) return true; return isInSubDirectory(dir, file.getParentFile()); }
除了来自rocketboy的asnwer之外,使用getAbsolutePath()
getCanonicalPath()
instad使\dir\dir2\..\file
转换为\dir\file
:
boolean areRelated = file.getCanonicalPath().contains(dir.getCanonicalPath() + File.separator); System.out.println(areRelated);
要么
boolean areRelated = child.getCanonicalPath().startsWith(parent.getCanonicalPath() + File.separator);
不要忘记通过try {...} catch {...}
捕获任何Exception
。
注意:您可以使用FileSystem.getSeparator()
而不是File.separator
。 执行此操作的“正确”方法是将要作为String
检查的目录的getCanonicalPath()
获取,然后检查是否以File.separator
结束,如果没有,则将File.separator
添加到结尾该String
,以避免双斜杠。 这样,如果Java决定最后返回带有斜杠的目录,或者如果您的目录字符串来自Java.io.File
以外的其他位置,则可以跳过将来的奇怪行为。
注意2:Thanx到@david指向File.separator
问题。
这个方法看起来很稳固:
/** * Checks, whether the child directory is a subdirectory of the base * directory. * * @param base the base directory. * @param child the suspected child directory. * @return true, if the child is a subdirectory of the base directory. * @throws IOException if an IOError occured during the test. */ public boolean isSubDirectory(File base, File child) throws IOException { base = base.getCanonicalFile(); child = child.getCanonicalFile(); File parentFile = child; while (parentFile != null) { if (base.equals(parentFile)) { return true; } parentFile = parentFile.getParentFile(); } return false; }
资源
它与dacwe的解决方案类似,但不使用递归(尽管在这种情况下不应该有很大的不同)。
如果您计划使用文件和文件名,请检查apache fileutils和filenameutils库。 充满了实用性(以及portale,如果可移植性是matdatory)function
public class Test { public static void main(String[] args) { File root = new File("c:\\test"); String fileName = "a.txt"; try { boolean recursive = true; Collection files = FileUtils.listFiles(root, null, recursive); for (Iterator iterator = files.iterator(); iterator.hasNext();) { File file = (File) iterator.next(); if (file.getName().equals(fileName)) System.out.println(file.getAbsolutePath()); } } catch (Exception e) { e.printStackTrace(); } }
}
您可以从特定DIR开始遍历文件树。 在Java 7中,有Files.walkFileTree
方法。 您只需编写自己的访问者来检查当前节点是否是搜索文件。 更多文档: http : //docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#walkFileTree%28java.nio.file.Path,%20java.util.Set,%20int ,%20java.nio.file.FileVisitor%29
比较路径怎么样?
boolean areRelated = file.getAbsolutePath().contains(dir.getAbsolutePath()); System.out.println(areRelated);
要么
boolean areRelated = child.getAbsolutePath().startsWith(parent.getAbsolutePath())