Java Directory Delete nio deleteDirectory(Path path)

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

Description

delete Directory

License

Open Source License

Declaration

public static void deleteDirectory(Path path) throws IOException 

Method Source Code


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

import java.io.*;

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

public class Main {
    public static void deleteDirectory(Path path) throws IOException {

        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override/*from w  w w.j ava 2 s . co  m*/
            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 {
                // try to delete the file anyway, even if its attributes
                // could not be read, since delete-only access is
                // theoretically possible
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

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

Related

  1. deleteDirectory(Path directory)
  2. deleteDirectory(Path directory)
  3. deleteDirectory(Path directory)
  4. deleteDirectory(Path directory)
  5. deleteDirectory(Path dirToDelete)
  6. deleteDirectory(String path)
  7. deleteDirectoryAndContents(String dir)
  8. deleteDirectoryRecursively(Path dir)
  9. deleteDirectoryRecursively(Path path)