Java File Path Delete DeleteDirectory(String directoryPath)

Here you can find the source of DeleteDirectory(String directoryPath)

Description

Remove a directory and all of its contents.

License

Open Source License

Return

true if the complete directory was removed, false if it could not be. If false is returned then some of the files in the directory may have been removed.

Declaration

public static boolean DeleteDirectory(String directoryPath) 

Method Source Code

//package com.java2s;
// modify it under the terms of the GNU General Public License

import java.io.File;

public class Main {
    /**//from   w w  w. j a  v  a  2s. c  o  m
     Remove a directory and all of its contents.

     The results of executing File.delete() on a File object
     that represents a directory seems to be platform
     dependent. This method removes the directory
     and all of its contents.

     @return true if the complete directory was removed, false if it could not be.
     If false is returned then some of the files in the directory may have been removed.

     */
    public static boolean DeleteDirectory(String directoryPath) {
        if (directoryPath == null || directoryPath.isEmpty()) {
            return false;
        }

        File directory = new File(directoryPath);

        if (directory == null)
            return false;
        if (!directory.exists())
            return true;
        if (!directory.isDirectory())
            return false;

        String[] list = directory.list();

        // Some JVMs return null for File.list() when the
        // directory is empty.
        if (list != null) {
            for (int i = 0; i < list.length; i++) {
                File entry = new File(directory, list[i]);

                //        System.out.println("\tremoving entry " + entry);

                if (entry.isDirectory()) {
                    if (!DeleteDirectory(entry.getPath()))
                        return false;
                } else {
                    if (!entry.delete())
                        return false;
                }
            }
        }

        return directory.delete();
    }
}

Related

  1. deleteDirectory(final String directoryPath)
  2. deleteDirectory(final String fullDirPath)
  3. deleteDirectory(final String path)
  4. deleteDirectory(String dir_path)
  5. deleteDirectory(String directoryPath)
  6. deleteDirectory(String dirPath)
  7. deleteDirectory(String dirPath)
  8. deleteDirectory(String filePath)
  9. deleteDirectory(String path)