Java Directory Delete nio deleteDirectoryAndContents(String dir)

Here you can find the source of deleteDirectoryAndContents(String dir)

Description

delete Directory And Contents

License

Open Source License

Declaration

public static void deleteDirectoryAndContents(String dir) throws IOException 

Method Source Code


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

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

public class Main {
    public static void deleteDirectoryAndContents(String dir) throws IOException {
        deleteDirectoryAndContents(Paths.get(dir));
    }/*  w  ww.java  2 s . com*/

    public static void deleteDirectoryAndContents(Path dir) throws IOException {
        cleanDirectory(dir);
        Files.delete(dir);
    }

    public static void cleanDirectory(String dir) throws IOException, InvalidPathException {
        cleanDirectory(Paths.get(dir));
    }

    public static void cleanDirectory(Path dir) throws IOException {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            for (Path file : stream) {
                if (Files.isDirectory(file)) {
                    cleanDirectory(file);
                }
                Files.delete(file);
            }
        } catch (DirectoryIteratorException x) {
            throw new IOException(x);
        }
    }
}

Related

  1. deleteDirectory(Path directory)
  2. deleteDirectory(Path directory)
  3. deleteDirectory(Path dirToDelete)
  4. deleteDirectory(Path path)
  5. deleteDirectory(String path)
  6. deleteDirectoryRecursively(Path dir)
  7. deleteDirectoryRecursively(Path path)
  8. deleteDirectorySelectively(Path path, Predicate predicate)
  9. deleteDirIfEmpty(Path dir)