Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.util.Arrays;
import java.util.List;


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

private static List<String> excludeFiles() {
String[] filesToBeExcluded = { "sri", "agnew" };
File sourceDirectory = new File(
String[] fileNames = sourceDirectory.list(new PrefixFileFilter(
List<String> listOfFiles = Arrays.asList(fileNames);
List<String> totalFiles = Arrays.asList(sourceDirectory.list());
return totalFiles;
share|improve this question
up vote 5 down vote accepted

you can use like:

    final List<String> excludedFiles = Arrays.asList(filesToBeExcluded);
    File[] totalFiles = sourceDirectory.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !excludedFiles.contains(name);
share|improve this answer
Thanks! It makes sense. – sriram Dec 11 '12 at 15:31

Java 7 have had new features in nio2 : Files et Paths, it manage also Charset problems.

Here he sample of DirectoryStream.Filter

static void newDirectoryStreamFilter(final Path file1, final Object filter1) {
        System.out.println(sepFin + "\nFiles.newDirectoryStreamFilter("
            + file1.getFileName() + "), filtre: " + filter1 + sepDeb);
        try {
            final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
                public boolean accept(final Path file1) throws IOException {
                    for (String s : filesToBeExcluded)
                        if (file1.startsWith(s)) return false;
                        return true;
            try {
                for (final Path path : Files.newDirectoryStream(file1, filter)) {
                    System.out.format("%10d %s\n", Files.size(path),
            } catch (final Exception e) {
        } catch (final Exception e) {
share|improve this answer

As you are already using, you could use the NotFileFilter.

and you will get:

private static List<String> getFileNamesWithoutExcludedPrefixes(File directory, String[] filesToBeExcluded) {
    return Arrays.asList(directory.list(new NotFileFilter(new PrefixFileFilter(filesToBeExcluded))));

(Argument checking could be added)

In your source:


this can not work, because Arrays.asList() creates an immutable list.

You could change the line that creates the list to:

new ArrayList<String>(...);

if you need mutability.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.