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.

What I am trying to do below is to remove files based on a 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 is 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;
}
}

Thanks, Sriram

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

you can use java.io.FilenameFilter like:

    File[] totalFiles = sourceDirectory.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            for (String s : filesToBeExcluded)
                if (name.startsWith(s)) return false;
            return true;
        }
    });

and make sure to mark filesToBeExcluded variable as final

share|improve this answer
    
Thanks! It makes sense. –  sriram Dec 11 '12 at 15:31
add comment

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
add comment

As you are already using commons.io, you could use the NotFileFilter: http://commons.apache.org/io/api-release/org/apache/commons/io/filefilter/NotFileFilter.html

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)

edit: oh and 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
add comment

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.