2
\$\begingroup\$

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;
}
}
\$\endgroup\$

3 Answers 3

5
\$\begingroup\$

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);
        }
    });
\$\endgroup\$
0
1
\$\begingroup\$

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());
        }
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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