List of utility methods to do Directory Delete nio
void | deleteDirOrFile(Path p, boolean followSymLinkDir) remove given file or dir. if (p == null || !Files.exists(p)) return; if (!Files.isDirectory(p)) Files.delete(p); else Files.walkFileTree(p, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override ... |
void | deleteDirReqursivelyIfExists(File dir) delete Dir Reqursively If Exists if (dir.exists()) {
deleteDirReqursively(dir);
|
boolean | deleteDirWithFiles(File dir, int maxDepth) delete Dir With Files File[] entries = dir.listFiles(); if (entries == null) return false; Stream.of(entries).filter(File::isDirectory).forEach(f -> { if (maxDepth < 1) { throw new AssertionError("Contains directory " + f); } else { deleteDirWithFiles(f, maxDepth - 1); ... |
void | deleteFileOrDirectory(File file) Delete a file or directory if (file.exists()) { if (file.isDirectory()) { Path rootPath = Paths.get(file.getAbsolutePath()); Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder()) .map(Path::toFile).forEach(File::delete); } else { file.delete(); } else { throw new RuntimeException("File or directory does not exist"); |
void | deleteFileOrDirectory(final File source) delete File Or Directory if (source.isFile()) { Path path = source.toPath(); try { Files.deleteIfExists(path); } catch (IOException | SecurityException e) { System.err.println(e); return; ... |
void | deleteFileOrFolder(final Path path) delete File Or Folder Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { Files.delete(file); return CONTINUE; @Override public FileVisitResult visitFileFailed(final Path file, final IOException e) { ... |