Java Delete File Recursively deleteRecursively(File dirEntry)

Here you can find the source of deleteRecursively(File dirEntry)

Description

Deletes a file or directory (recursively).

License

Apache License

Parameter

Parameter Description
dirEntry A file or directory to be deleted.

Exception

Parameter Description
IOException Thrown if deletion fails.

Declaration

public static void deleteRecursively(File dirEntry) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File;

import java.io.IOException;

public class Main {
    /**/*from  w  ww .java2 s . co  m*/
     * Deletes a file or directory (recursively).
     * 
     * @param dirEntry
     *            A file or directory to be deleted.
     * @throws IOException
     *             Thrown if deletion fails.
     */
    public static void deleteRecursively(File dirEntry) throws IOException {
        checkNotNull(dirEntry, "null directory entry");

        if (!dirEntry.exists()) {
            return;
        }
        if (dirEntry.isDirectory()) {
            File[] files = dirEntry.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteRecursively(file);
                }
            }
        }
        if (!dirEntry.delete()) {
            throw new IOException("Failed to delete file: " + dirEntry.getAbsolutePath());
        }
    }
}

Related

  1. deleteRecursiveDirectories(String mainDir)
  2. deleteRecursiveDirectory(String dirName)
  3. deleteRecursiveFileDir(File dir)
  4. deleteRecursively(File dir)
  5. deleteRecursively(File directory)
  6. deleteRecursively(File f)
  7. deleteRecursively(File f)
  8. deleteRecursively(File file)
  9. deleteRecursively(File file)