List of utility methods to do Directory Delete nio
boolean | deleteDirectory(Path directory) Deletes a directory recursively if (directory != null) { try { Files.walkFileTree(directory, 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 { Files.delete(dir); return FileVisitResult.CONTINUE; }); } catch (IOException ignored) { return false; return true; |
void | deleteDirectory(Path dirToDelete) delete Directory if (!Files.isDirectory(dirToDelete)) throw new IllegalArgumentException("the path is not a directory"); Files.walkFileTree(dirToDelete, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("Deleting file: " + file); Files.delete(file); return CONTINUE; ... |
void | deleteDirectory(Path path) delete Directory Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { ... |
void | deleteDirectory(String path) delete Directory File file = new File(path); File[] contents = file.listFiles(); if (contents != null) { for (File f : contents) { f.delete(); file.delete(); ... |
void | deleteDirectoryAndContents(String dir) delete Directory And Contents deleteDirectoryAndContents(Paths.get(dir)); |
void | deleteDirectoryRecursively(Path dir) Delete a directory recursively. if (!Files.exists(dir)) { return; Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; ... |
void | deleteDirectoryRecursively(Path path) delete Directory Recursively Files.walkFileTree(path, new FileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { ... |
void | deleteDirectorySelectively(Path path, Predicate delete Directory Selectively Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { if (predicate.test(file)) { Files.delete(file); return FileVisitResult.CONTINUE; @Override public FileVisitResult postVisitDirectory(Path directory, IOException ex) throws IOException { if (ex != null) { return FileVisitResult.TERMINATE; if (predicate.test(directory)) { Files.delete(directory); return FileVisitResult.CONTINUE; }); |
void | deleteDirIfEmpty(Path dir) delete Dir If Empty if (Files.isDirectory(dir)) { try { if (!Files.list(dir).findFirst().isPresent()) Files.deleteIfExists(dir); } catch (DirectoryNotEmptyException | NoSuchFileException ignored) { |
void | deleteDirIfExists(Path dirPath) delete Dir If Exists if (Files.exists(dirPath)) {
deleteDir(dirPath);
|