File-exclusion based on an array of prefixes I am trying to remove files based on an array of prefixes, from files present in sourceDirectory. I am using prefixFileFilter to get the list of files with the prefixes. After that I am removing these from the original list.

Is there any better approach?

package sriram;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.filefilter.PrefixFileFilter;

public class FileFilterExample {
public static void main(String[] args) {
excludeFiles();
}

private static List&lt;String&gt; excludeFiles() {
String[] filesToBeExcluded = { "sri", "agnew" };
File sourceDirectory = new File(
        "C:\\Users\\sriram\\workspace1\\sample\\src");
String[] fileNames = sourceDirectory.list(new PrefixFileFilter(
        filesToBeExcluded));
List&lt;String&gt; listOfFiles = Arrays.asList(fileNames);
List&lt;String&gt; totalFiles = Arrays.asList(sourceDirectory.list());
totalFiles.removeAll(listOfFiles);
return totalFiles;
}
}
you can use java.io.FilenameFilter like:

    final List&lt;String&gt; excludedFiles = Arrays.asList(filesToBeExcluded);
    File[] totalFiles = sourceDirectory.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return !excludedFiles.contains(name);
        }
    });