Java: list files recursively in subdirectories with Apache commons-IO 2.4

こ雲淡風輕ζ 提交于 2019-12-06 03:04:38

问题


I'm trying to list files with the extension ".xls" in the root directory and its sub-directories, using the following code with Apache Commons-IO 2.4 library. I am checking the size of the collection<File>, but it gives 0. I don't see where could be wrong in the code. Could you please help me with this?

public static void main(String[] args) throws IOException {

    File rootDir = new File(args[0]);

    Collection<File> files = FileUtils.listFiles(rootDir, new RegexFileFilter("[a-zA-Z].xls"), DirectoryFileFilter.DIRECTORY);

    System.out.println("collection size:" + files.size());

}

回答1:


I found this works:

    final String[] SUFFIX = {"xls"};  // use the suffix to filter

    File rootDir = new File(args[0]);

    Collection<File> files = FileUtils.listFiles(rootDir, SUFFIX, true);


来源:https://stackoverflow.com/questions/19776063/java-list-files-recursively-in-subdirectories-with-apache-commons-io-2-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!