Java Utililty Methods Directory Delete nio

List of utility methods to do Directory Delete nio

Description

The list of methods to do Directory Delete nio are organized into topic(s).

Method

voiddeleteDirOrFile(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
...
voiddeleteDirReqursivelyIfExists(File dir)
delete Dir Reqursively If Exists
if (dir.exists()) {
    deleteDirReqursively(dir);
booleandeleteDirWithFiles(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);
...
voiddeleteFileOrDirectory(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");
voiddeleteFileOrDirectory(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;
...
voiddeleteFileOrFolder(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) {
...