Java copies folder excluding some internal file

I have a folder with this structure

Main folder

   --Sub1  
         --File .scl
         --File .awl
         --Other files
   --Sub2  
         --Files
   --Sub3
   --Sub4

I want to copy it to another place, but I want Sub3 to be avoided and (depending on the situation) some file from Sub1

Here is an excerpt from what I have done so far:

FileUtils.copyDirectory(srcDir, dstDir, new FileFilter() {
        public boolean accept(File pathname) {
            // We don't want 'Sub3' folder to be imported
            // + look at the settings to decide if some format needs to be
            // excluded
            String[] ignoreList= new String[]{
                    !Settings.getSiemensOptionAWL() ? ".awl":"uselessStringWilNeverBeFound",
                    !Settings.getSiemensOptionSCL() ? ".scl":"uselessStringWilNeverBeFound",
                    "Sub3"
            };

            return !(ignoreFile(pathname, ignoreList) && pathname
                    .isDirectory());
        }
    }, true);


    public static boolean ignoreFile(File file, String[] ignoreList) {
        for (final String ignoreStr : ignoreList)
            if (file.getAbsolutePath().contains(ignoreStr))
                return true;
        return false;
    }

Apparently, he works for a job. But I think this is a very ugly solution .... Does anyone know a better way?

PS: of course Settings.getSiemensOptionAWL () is just a boolean function returning my solution

+5
source share
4 answers

The other options offered here are good, however another alternative is to combine several simple FileFilters together (which may be unnecessary, of course!)

public class FailFastFileFilter implements FileFilter {
    protected final List<FileFilter> children = new ArrayList<FileFilter>();

    public FailFastFileFilter(FileFilter... filters) {
        for (FileFilter filter: filters) {
            if (filter != null)
                this.filters.add(filter);
        }       
    }

    public boolean accept(File pathname) {
        for (FileFilter filter: this.filters) {
            if (!filter.accept(pathname)) {
                return false; // fail on the first reject
            }
        }

        return true;
    }
}

, FileFilters Sub3,.scl case.awl. FailFastFileFilter, , null ( inline if , FileFilters)

, Sub1 Sub3.

:

public class ExcludeExtensionInDirFileFilter implements FileFilter {
    protected final String parentFolder;
    protected final String extension;

    public ExtensionFileFilter(String parentFolder, String extension) {
        this.parentFolder = parentFolder;
        this.extension = extension.toLowerCase();
    }

    public boolean accept(File file) {
        if (!file.isDirectory() && file.getParentFile().getName().equalsIgnoreCase(parentFolder))
            return !file.getAbsolutePath().toLowerCase().endsWith(extension);
        else
            return true;
    }
}

, :

public class ExcludeDirFileFilter implements FileFilter {
    protected final String name;

    public ExcludeDirFileFilter(String name) {
        this.name = name.toLowerCase();
    }

    public boolean accept(File file) {
        if (file.isDirectory() && file.getName().equalsIgnoreCase(name))
            return false;
        else
            return true;
    }
}

FailFastFileFilter :

FileFilter filters = new FailFastFileFilter(
    new ExcludeDirFileFilter("Sub3"), // always exclude Sub3
    (!Settings.getSiemensOptionAWL() ? new ExcludeExtensionInDirFileFilter("Sub1",".awl"), null), // Exclude Sub1/*.awl if desired
    (!Settings.getSiemensOptionSCL() ? new ExcludeExtensionInDirFileFilter("Sub1",".scl"), null) // Exclude Sub1/*.scl if desired
);

FileUtils.copyDirectory(srcDir, dstDir, filters);
+4

, ignoreFile(), ( , ..). , , . - :

FileUtils.copyDirectory(srcDir, dstDir, new FileFilter() {
    public boolean accept(File pathname) {
        // We don't want 'Sub3' folder to be imported
        // + look at the settings to decide if some format needs to be
        // excluded
        String name = pathname.getName();
        if (!Settings.getSiemensOptionAWL() && name.endsWith(".awl"))
            return false;
        if (!Settings.getSiemensOptionSCL() && name.endsWith(".scl"))
            return false;

        return !(name.equals("Sub3") && pathname.isDirectory());
    }
}, true);
+3

? , -

new FileFilter() {
    public boolean accept(File pathname) {
        String path = pathname.getAbsolutePath().toLowerCase();

        return (!pathname.isDirectory() || path.endsWith("sub3")) &&
            (!Settings.getSiemensOptionAWL() && path.endsWith(".awl")) &&
            (!Settings.getSiemensOptionSCL() && path.endsWith(".scl"));
    }
}
+1

. , . CopySubDir, . .

-1

All Articles