Java File Path Delete deleteFolderRec(File path, boolean alsoDeleteGivenFolder)

Here you can find the source of deleteFolderRec(File path, boolean alsoDeleteGivenFolder)

Description

This function will recursively delete directories and files.

License

Open Source License

Parameter

Parameter Description
path Directory to be deleted
alsoDeleteGivenFolder Flag, indicating whether the directory that was passed in as first argument shall be deleted along with its contents or not.

Return

true if deletion was successfully completed,
false otherwise. Parts of the given folder might already be deleted.

Declaration

public static boolean deleteFolderRec(File path, boolean alsoDeleteGivenFolder) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**//from   w  ww.  ja  v a 2  s . com
     * This function will recursively delete directories and files.
     * 
     * @param path Directory to be deleted
     * @param alsoDeleteGivenFolder Flag, indicating whether the directory that 
     * was passed in as first argument shall be deleted along with its contents 
     * or not. 
     * @return <tt>true</tt> if deletion was successfully completed,<br>
     * <tt>false</tt> otherwise. Parts of the given folder might already be deleted. 
     */
    public static boolean deleteFolderRec(File path, boolean alsoDeleteGivenFolder) {

        boolean ok;

        if (path.exists()) {
            ok = true;
            if (path.isDirectory()) {
                File[] files = path.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        deleteFolderRec(files[i], true);
                    } else {
                        files[i].delete();
                    }
                }
                if (alsoDeleteGivenFolder) {
                    ok = ok && path.delete();
                }
            }
        } else {
            ok = false;
        }

        return ok;
    }
}

Related

  1. deleteFolder(String path)
  2. deleteFolder(String pathToRootFolder)
  3. deleteFolder(String pFolderPath)
  4. deleteFolder(String sPath)
  5. deleteFolderAndContent(String folderPath)
  6. deleteFolderRecursively(File path, boolean includeSelf)
  7. deleteIfExist(String filePath)
  8. deleteInPathTempFiles(String workingFolder)
  9. deleteInRemote(String host, String path)