Java Path Delete nio deleteNotEmptyDirectory(Path path)

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

Description

delete Not Empty Directory

License

Apache License

Declaration

public static void deleteNotEmptyDirectory(Path path)
            throws IOException 

Method Source Code

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

import static java.nio.file.FileVisitResult.CONTINUE;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void deleteNotEmptyDirectory(Path path)
            throws IOException {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override//from   w  w w. j  av a 2s  . c o m
            public FileVisitResult visitFile(Path file,
                    BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir,
                    IOException exc) throws IOException {
                if (exc == null) {
                    Files.delete(dir);
                    return CONTINUE;
                } else {
                    throw exc;
                }
            }
        });
    }
}

Related

  1. deleteFilesRecursively(Path path, final PathMatcher pathMatcher)
  2. deleteIfExists(Path thePath)
  3. deleteIfExists(Path value)
  4. deleteIndexBeforeStart(String basePath)
  5. deleteLockFile(Path logFile)
  6. deleteOnExit(Path path)
  7. deletePath(Path path)
  8. deletePathRecursively(String path)
  9. deleteQuietly(@Nullable Path path)