Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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<String> excludeFiles() {
String[] filesToBeExcluded = { "sri", "agnew" };
File sourceDirectory = new File(
        "C:\\Users\\sriram\\workspace1\\sample\\src");
String[] fileNames = sourceDirectory.list(new PrefixFileFilter(
        filesToBeExcluded));
List<String> listOfFiles = Arrays.asList(fileNames);
List<String> totalFiles = Arrays.asList(sourceDirectory.list());
totalFiles.removeAll(listOfFiles);
return totalFiles;
}
}
share|improve this question

3 Answers 3

up vote 5 down vote accepted

you can use java.io.FilenameFilter like:

    final List<String> excludedFiles = Arrays.asList(filesToBeExcluded);
    File[] totalFiles = sourceDirectory.listFiles(new FilenameFilter() {
        @Override
        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>() {
                @Override
                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),
                        path.toAbsolutePath());
                }
            } catch (final Exception e) {
                System.err.println(e.getMessage());
            }
        } catch (final Exception e) {
            System.err.println(e.getMessage());
        }
share|improve this answer

As you are already using commons.io, 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:

totalFiles.removeAll(listOfFiles);

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

 
discard

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.