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

1

5

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());

}
Upspring answered 4/11, 2013 at 20:2 Comment(2)
Your regular expression does only match one character before the extension?Dallis
oh, good call. I should have used [a-zA-Z]*Upspring
U
11

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);
Upspring answered 4/11, 2013 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.