I have a folder with this structure
mainFolder
--Sub1
--File .scl
--File .awl
--Other files
--Sub2
--Fi
The other options suggested here are good, however another alternative is to nest multiple simpler FileFilters together (which may be overkill, of course!)
public class FailFastFileFilter implements FileFilter {
protected final List children = new ArrayList();
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;
}
}
Then simply combine short, concise FileFilters for the Sub3 case, the .scl and the .awl case. The example FailFastFileFilter I've shown above would let you specify null as one of the filters (so you could use inline if statements to determine whether particular FileFilters are applied)
For the sake of completion, here's a general idea of how I'd implement the child filters for the Sub1 cases and the Sub3 case.
First, a filter to excluding files with a particular extension within a directory:
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;
}
}
Then to exclude a directory:
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;
}
}
Setting up the FailFastFileFilter would then look something like:
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);