FileUtils.iterateFiles IGNORECASE

7k views Asked by At

I need to find files in directories and subdirectories by type of extension and ignorecase mode, this is my code:

String[] patron = {"doc", "xls", "ppt"};
Iterator iter =  FileUtils.iterateFiles(new File(directories), patron, true);

I also need to return the files with extension like Doc, Doc, ... ignorecase

3

There are 3 answers

2
Anas On

Use the other version of the method and pass your own filter:

String[] extensions = new String[] {"doc", "xls", "ppt"};
IOFileFilter filter = new SuffixFileFilter(extensions, IOCase.INSENSITIVE);
Iterator iter = FileUtils.iterateFiles(new File(directorio), filter, DirectoryFileFilter.DIRECTORY);

Notice the use of IOCase.INSENSITIVE.

0
AurA On

You must refer the documentation of FileUtils at http://commons.apache.org/proper/commons-io/javadocs/api-1.4/org/apache/commons/io/FileUtils.html

The definition you are using

public static Iterator iterateFiles(File directory,
                                    String[] extensions,
                                    boolean recursive)

Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions. This method is based on listFiles(File, String[], boolean).

extensions - an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.recursive - if true all subdirectories are searched as well

it should return files irrespective of doc or DOc or dOC they are all same.

Else there is one more definition given where you can apply filters.

You can use this definition for iterateFiles

iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)

In this you have to pass an IOFileFilter object, you can implement the given accept method definition

boolean accept(File dir, String name)

The documentation of IOFileFilter can be found at http://commons.apache.org/proper/commons-io/javadocs/api-1.4/org/apache/commons/io/filefilter/IOFileFilter.html

0
Jay On

I actually wanted to comment on @AurA's answer but I am not allowed to because it says "You must have 50 reputation to comment".

listFiles(File, String[], boolean)

is case-sensitive, so it does NOT "return files irrespective of doc or DOc or dOC".