mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 11:04:34 +02:00
8144009: ToolBox should have a cleanDirectory method
Added cleanDirectory method to ToolBox. Reviewed-by: jjg
This commit is contained in:
parent
68126c8ee9
commit
ea92f6bf06
14 changed files with 36 additions and 71 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue