I am trying to go through a file tree and delete all files / directories. Code below:
Files.walkFileTree(metricPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { throw exc; } } }); }
This code runs between unit tests, each of which generates a separate file in the form of folder1/folder2/file . When I try to traverse this tree, The DirectoryNotEmptyException is thrown when folder 1 tries to delete, although it is clearly empty ...
source share