为了获得包含在一个指定的目录,并根据一些扩展的所有文件,我使用的方法listFiles类的FileUtils从阿帕奇百科全书IO库,如下面的代码样本.
ArrayList<String> wildcards = new ArrayList<>();
wildcards.add("*.cpp");
wildcards.add("*.h");
wildcards.add("*.txt");
File dir = new File("/path/to/dir");
Collection<File> found = FileUtils.listFiles(
dir,
new WildcardFileFilter(wildcards, IOCase.SENSITIVE),
DirectoryFileFilter.DIRECTORY);
List<File> files = new ArrayList<>(found);
Run Code Online (Sandbox Code Playgroud)
结果中的项目顺序Collection<File>在不同的操作系统中有所不同,因此我会files根据以下规则对它们(即包装列表)进行排序.
例:
/path/to/dir/first/subpath/main.cpp
/path/to/dir/first/subpath/utils.cpp
/path/to/dir/first/subpath/utils.h
/path/to/dir/first/main.cpp
/path/to/dir/first/utils.cpp
/path/to/dir/first/utils.h
/path/to/dir/second/main.cpp
/path/to/dir/second/utils.cpp
/path/to/dir/second/utils.h
/path/to/dir/README.txt
Run Code Online (Sandbox Code Playgroud)