使用apache commons fileutils排除特定的子目录 [英] Exclude specific sub-directories using apache commons fileutils
问题描述
我想使用apache commons lib列出当前目录下的所有文件,但不包括子目录及其子目录中的所有文件.
例如:如果我当前的目录是-A,其子目录为B,C,D
B的子目录为b1,b2(b1的子目录为b12),C的子目录为c1,c2 ...现在我想列出C,c1,c2,D中的所有文件(不包括B,b1,b12,b2)
任何帮助将不胜感激!
使用org.apache.commons.io.FileFilter,其中有一个 I want to list all the files under current directory but excluding all the files within the subdirectory and it's subdirectories using apache commons lib. For Example : If my current directory is -- A and its subdirectory as B, C ,D Any help will be highly appreciated! Use org.apache.commons.io.FileFilter, there's a good example of how to use it in the JavaDoc. For example I have a directory structure like: Then I could list only the files under C and D with the following code:
这篇关于使用apache commons fileutils排除特定的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
B having subdirectory as b1,b2 (b1 has b12 as its subdirectory) and C having c1 , c2...
now I want to list all the files in C,c1,c2, D (excluding B , b1 ,b12 , b2 )/tmp/testFilter/A
/tmp/testFilter/B
/tmp/testFilter/C/c1
/tmp/testFilter/C/c2
/tmp/testFilter/D/d1
public class FileLister {
public static void main(String args[]) {
File dir = new File("/tmp/testFilter");
String[] files = dir.list(
new NotFileFilter(
new OrFileFilter(
new PrefixFileFilter("A"),
new PrefixFileFilter("B")
)
)
);
listFiles(dir, files);
}
private static void listFiles(File rootDir, String[] files) {
for (String fileName: files) {
File fileOrDir = new File(rootDir, fileName);
if (fileOrDir.isDirectory()) {
listFiles(fileOrDir, fileOrDir.list());
} else {
System.out.println(fileOrDir);
}
}
}
}