Java Directory Delete nio deleteDirectory(File directory)

Here you can find the source of deleteDirectory(File directory)

Description

delete Directory

License

Apache License

Declaration

public static void deleteDirectory(File directory) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void deleteDirectory(File directory) throws IOException {
        cleanDirectory(directory);//  ww  w  .j a va 2s.co m
        Files.delete(Paths.get(directory.getAbsolutePath()));
    }

    public static void cleanDirectory(File directory) throws IOException {
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException(directory + " is not a directory");
        }

        File[] files = directory.listFiles();
        if (files == null) {
            throw new IOException("Failed to list contents of " + directory);
        }

        for (File file : files) {
            delete(file);
        }
    }

    public static void delete(File file) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);
            return;
        }
        Files.delete(Paths.get(file.getPath()));
    }
}

Related

  1. deleteDirectory(File dir)
  2. deleteDirectory(File dir)
  3. deleteDirectory(File directory)
  4. deleteDirectory(File directory)
  5. deleteDirectory(File directory)
  6. deleteDirectory(File directory)
  7. deleteDirectory(File file)
  8. deleteDirectory(File path)
  9. deleteDirectory(final File directory)