I have a list of all the files & directories that I need to delete, but I need to exclude all files/directories that match a given glob pattern. Any suggestions on how I can achieve this?
I am currently using the FileUtils class to delete files like this -
for (File path : cleanableFiles) {
try {
FileUtils.deleteQuietly(path);
FileUtils.deleteDirectory(path);
} catch (Exception exception) {
}
}
cleanableFiles is Set<File>. I also have a glob pattern string (e.g '*.txt' to match all files that have .txt extension). I need to not delete the files/directories that match the glob pattern.
You can accomplish this in Java by using the java.nio.file.Files and java.nio.file.Path classes, and by using the java.nio.file.FileSystem class to create a PathMatcher that can match against a glob pattern.