Java Directory Clear cleanDirectory(File directory)

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

Description

clean Directory

License

Apache License

Declaration

public static void cleanDirectory(File directory) throws IOException 

Method Source Code

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

import java.io.File;

import java.io.IOException;

public class Main {

    public static void cleanDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }/*from w w  w.  ja v  a  2  s.c o  m*/

        if (!directory.isDirectory()) {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

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

        IOException exception = null;
        for (File file : files) {
            try {
                delete(file);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }

        if (null != exception) {
            throw exception;
        }
    }

    public static void delete(File fileOrDir) throws IOException {
        if (fileOrDir == null) {
            return;
        }

        if (fileOrDir.isDirectory()) {
            cleanDirectory(fileOrDir);
        }

        fileOrDir.delete();
    }
}

Related

  1. cleanDirectory(File dir)
  2. cleanDirectory(File dir, boolean deleteDirectory)
  3. cleanDirectory(File directory)
  4. cleanDirectory(File directory)
  5. cleanDirectory(File directory)
  6. cleanDirectory(File directory)
  7. cleanDirectory(File directory)
  8. cleanDirectory(File directory)
  9. cleanDirectory(File directory)