gstackoverflow
gstackoverflow

Reputation: 37088

apache commons I/O. How to filter files by wildcard?

I have following files in directory:

error.log
error1.log
Somename.log.trololo
AnotherName.logarifm

I want that following files will be in array:

    error.log
    error1.log
    Somename.log.trololo

I wrote following code:

FileFilter fileFilter = new WildcardFileFilter("*.log.*|*.log$");
File[] files = source.listFiles(fileFilter);

But my array is empty.

How to write right wildcard?

UPDATE

public class Test {
    public static void main(String[] args) {
        File source = new File("D:");
        if (source.isDirectory()) {
            FileFilter fileFilter = new WildcardFileFilter(Arrays.asList("*.log.*", "*.log"));
            File[] files = source.listFiles(fileFilter);
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i]);
            }
        }
    }
}

enter image description here

Upvotes: 2

Views: 4910

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280122

You'll want

FileFilter fileFilter = new WildcardFileFilter(Arrays.asList("*.log.*", "*.log"));

The WildcardFileFilter doesn't work with regular expressions. It works with UNIX style wildcard expressions. * for any number of chracters and ? for one character.

Upvotes: 2

Related Questions