Java File Path Delete deleteDirectory(File path)

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

Description

Delete a non-empty directory Copied from http://www.rgagnon.com/javadetails/java-0483.html

License

Open Source License

Parameter

Parameter Description
path a parameter

Return

true if and only if the file or directory is successfully deleted; false otherwise

Declaration

public static boolean deleteDirectory(File path) 

Method Source Code


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

import java.io.*;

public class Main {
    /**/* w w  w  . j  a  v a  2  s .  c o  m*/
     * Delete a non-empty directory
     * Copied from http://www.rgagnon.com/javadetails/java-0483.html
     * @param path
     * @return true if and only if the file or directory is successfully deleted; false otherwise
     */
    public static boolean deleteDirectory(File path) {
        boolean succeeded = true;

        // first empty it
        if (path.exists()) {
            File[] files = path.listFiles();

            if (files != null) {
                for (int i = 0; succeeded && (i < files.length); i++) {
                    if (files[i].isDirectory()) {
                        succeeded &= deleteDirectory(files[i]);
                    } else {
                        succeeded &= files[i].delete();
                    }
                }
            }
        }
        // then delete it
        if (succeeded)
            succeeded &= path.delete();
        return succeeded;
    }
}

Related

  1. deleteDirectory(File path)
  2. deleteDirectory(File path)
  3. deleteDirectory(File path)
  4. deleteDirectory(File path)
  5. deleteDirectory(File path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path, boolean deleteMyself, boolean deleteHidden)
  8. deleteDirectory(File path, boolean includeDir)
  9. deleteDirectory(final File path)