September 2019
Intermediate to advanced
816 pages
18h 47m
English
Before attempting to delete a folder, we must delete all the files from it. This statement is very important since it doesn't allow us to simply call the delete()/deleteIfExists() methods for a folder that contains files. An elegant solution to this problem relies on a FileVisitor implementation that starts from the following stub:
public class DeleteFileVisitor implements FileVisitor { ... private static boolean delete(Path file) throws IOException { return Files.deleteIfExists(file); }}
Let's take a look at the main checkpoints and the implementation of deleting a folder: