Java Delete Directory 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.*;

public class Main {
    public static void deleteDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            return;
        }/*from ww  w .j  av  a 2s. co  m*/

        forceDelete(directory);

        if (!directory.delete()) {
            throw new IOException("Unable to delete directory " + directory + ".");
        }
    }

    private static void forceDelete(File file) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File child : files) {
                forceDelete(child);
            }
            deleteFile(file);
        } else {
            deleteFile(file);
        }
    }

    public static void deleteFile(File file) throws IOException {
        if (file == null) {
            return;
        }
        if (!file.delete()) {
            throw new IOException("Unable to delete file: " + file);
        }
    }
}

Related

  1. deleteDirectory(File directory)
  2. deleteDirectory(File directory)
  3. deleteDirectory(File directory)
  4. deleteDirectory(File directory)
  5. deleteDirectory(File directory)
  6. deleteDirectory(File directory)
  7. deleteDirectory(File directory)
  8. deleteDirectory(File directory)
  9. deleteDirectory(File directory)