Reputation: 11
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.
Upvotes: 0
Views: 168
Reputation:
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.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.PathMatcher;
import java.nio.file.FileSystems;
import java.io.IOException;
import java.util.List;
Upvotes: 0