Java File Path Delete deleteDirectory(final String directoryPath)

Here you can find the source of deleteDirectory(final String directoryPath)

Description

delete Directory

License

Open Source License

Declaration

public static boolean deleteDirectory(final String directoryPath) 

Method Source Code

//package com.java2s;
// Licensed under the MIT license. See License.txt in the repository root.

import java.io.File;
import java.io.FileFilter;

public class Main {
    public static boolean deleteDirectory(final String directoryPath) {
        return deleteDirectory(new File(directoryPath));
    }//  w  ww . j a v  a  2 s  . c om

    public static boolean deleteDirectory(final File directory) {
        if (!directory.exists() || !directory.isDirectory()) {
            return false;
        }

        /* Depth-first directory traversal */
        final File[] subdirectories = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {
                return file.isDirectory();
            }
        });

        for (final File subdirectory : subdirectories) {
            if (!deleteDirectory(subdirectory)) {
                return false;
            }
        }

        final File[] files = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {
                return file.isFile();
            }
        });

        for (final File file : files) {
            if (!file.delete()) {
                return false;
            }
        }

        return directory.delete();
    }
}

Related

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