Java Directory Delete nio deleteDirectoryRecursively(Path path)

Here you can find the source of deleteDirectoryRecursively(Path path)

Description

delete Directory Recursively

License

Apache License

Declaration

public static void deleteDirectoryRecursively(Path path) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void deleteDirectoryRecursively(Path path) throws IOException {
        Files.walkFileTree(path, new FileVisitor<Path>() {

            @Override//from   w w w.  ja va 2  s . co  m
            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 {
                return FileVisitResult.CONTINUE;
            }

            @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 {
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

Related

  1. deleteDirectory(Path dirToDelete)
  2. deleteDirectory(Path path)
  3. deleteDirectory(String path)
  4. deleteDirectoryAndContents(String dir)
  5. deleteDirectoryRecursively(Path dir)
  6. deleteDirectorySelectively(Path path, Predicate predicate)
  7. deleteDirIfEmpty(Path dir)
  8. deleteDirIfExists(Path dirPath)
  9. deleteDirOrFile(Path p, boolean followSymLinkDir)