How to delete directory in Java
If we use the NIO Files.delete to delete a non-empty directory in Java, it throws DirectoryNotEmptyException; for legacy IO File.delete to delete a non-empty directory, it returns a false. The standard solution is to loop the directory recursively, and delete all its children’s contents first (sub-files or sub-directories), and delete the parent later. This example shows some common ways to delete a directory in Java: (1) Files.walkFileTree + FileVisitor (Java 7), (2) Files.walk (Java 8), (3) FileUtils.deleteDirectory (Apache Common IO), (4) Recursive delete in a directory (Plain Java code).