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