Java File Path Delete deleteFolderRecursively(File path, boolean includeSelf)

Here you can find the source of deleteFolderRecursively(File path, boolean includeSelf)

Description

Deletes directory at path recursively.

License

Open Source License

Parameter

Parameter Description
path a parameter
includeSelf also delete the passed directory itself?

Exception

Parameter Description
IOException an exception

Declaration

public static void deleteFolderRecursively(File path, boolean includeSelf) throws IOException 

Method Source Code


//package com.java2s;

import java.io.File;

import java.io.IOException;

public class Main {
    /**//from w ww . j  av a  2 s  . c om
     * Deletes directory at path recursively.
     * 
     * @param path
     * @param includeSelf also delete the passed directory itself?
     * 
     * @throws IOException 
     */
    public static void deleteFolderRecursively(File path, boolean includeSelf) throws IOException {
        if (!path.isDirectory())
            throw new IllegalArgumentException("The passed file is not a directory.");

        File[] files = path.listFiles();

        if (files == null)
            return;

        for (int i = 0; i < files.length; i++) {
            File f = files[i];

            if (f.isFile()) {
                if (!f.delete()) {
                    throw new IOException("Couldn't delete file: " + f.getAbsolutePath());
                }
            } else {
                deleteFolderRecursively(f, true);
            }
        }

        if (includeSelf && !path.delete()) {
            throw new IOException("Couldn't delete directory: " + path.getAbsolutePath());
        }
    }

    /**
     * Deletes directory at path recursively.
     * 
     * @param path
     * @param includeSelf also delete the passed directory itself?
     * 
     * @throws IOException 
     */
    public static void deleteFolderRecursively(String path, boolean includeSelf) throws IOException {
        deleteFolderRecursively(new File(path), includeSelf);
    }

    /**
     * Deletes directory at path recursively.
     * 
     * @param path
     * 
     * @throws IOException 
     */
    public static void deleteFolderRecursively(File path) throws IOException {
        deleteFolderRecursively(path, true);
    }

    /**
     * Deletes directory at path recursively.
     * 
     * @param path
     * 
     * @throws IOException 
     */
    public static void deleteFolderRecursively(String path) throws IOException {
        deleteFolderRecursively(new File(path), true);
    }
}

Related

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