Java File Path Delete deleteNonEmptyDirectory(String dirPath)

Here you can find the source of deleteNonEmptyDirectory(String dirPath)

Description

Deletes a non-empty directory, defined by dirPath from the file system.

License

Open Source License

Parameter

Parameter Description
dirPath a parameter

Declaration

public static boolean deleteNonEmptyDirectory(String dirPath) 

Method Source Code


//package com.java2s;
import java.io.File;

public class Main {
    /**/*ww  w  .  ja v a2  s.c  om*/
     * Deletes a non-empty directory, defined by dirPath from the file system.
     * Returns true, if directory and all the files, contained in it, have been
     * deleted.
     * 
     * @param dirPath
     * @return
     */
    public static boolean deleteNonEmptyDirectory(String dirPath) {
        File dir = new File(dirPath);
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                boolean fileDeleted = file.delete();
                if (!fileDeleted)
                    return false;
            }
            return dir.delete();
        } else
            return false;
    }
}

Related

  1. deleteInPathTempFiles(String workingFolder)
  2. deleteInRemote(String host, String path)
  3. deleteJaasFile(String jaasPath)
  4. deleteKey(String path, String key)
  5. deleteMultipleFiles(String filePath, int numberOfFiles)
  6. deleteOldStorageFile(String filepath)
  7. deleteParent(String filePath)
  8. deletePath(File dirPath)
  9. deletePath(File path)