Java Path Delete nio deleteRecursive(Path path)

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

Description

Recursively delete the given path

License

Open Source License

Parameter

Parameter Description
path path to delete

Exception

Parameter Description
IOException if the path can not be visited

Declaration

public static void deleteRecursive(Path path) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//  w  w w .  j ava 2 s .  co  m
     * Recursively delete the given path
     * @param path path to delete
     * @throws IOException if the path can not be visited
     */
    public static void deleteRecursive(Path path) throws IOException {
        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 postVisitDirectory(Path dir, IOException e) throws IOException {
                if (e == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    // directory iteration failed
                    throw e;
                }
            }
        });
    }
}

Related

  1. deleteOnExit(Path path)
  2. deletePath(Path path)
  3. deletePathRecursively(String path)
  4. deleteQuietly(@Nullable Path path)
  5. deleteQuietly(Path dir)
  6. deleteRecursive(Path path)
  7. deleteRecursively(final Path path)
  8. deleteRecursively(Path directory)
  9. deleteRecursively(Path path)