8144009: ToolBox should have a cleanDirectory method

Added cleanDirectory method to ToolBox.

Reviewed-by: jjg
This commit is contained in:
Andreas Lundblad 2015-11-26 09:33:41 +01:00
parent 68126c8ee9
commit ea92f6bf06
14 changed files with 36 additions and 71 deletions

View file

@ -273,6 +273,34 @@ public class ToolBox {
Files.delete(Paths.get(file));
}
/**
* Deletes all content of a directory (but not the directory itself).
* @param root the directory to be cleaned
*/
public void cleanDirectory(Path root) throws IOException {
if (!Files.isDirectory(root)) {
throw new IOException(root + " is not a directory");
}
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes a) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
if (e != null) {
throw e;
}
if (!dir.equals(root)) {
Files.delete(dir);
}
return FileVisitResult.CONTINUE;
}
});
}
/**
* Moves a file.
* If the given destination exists and is a directory, the file will be moved