Java File Path Delete deleteDir(File dir)

Here you can find the source of deleteDir(File dir)

Description

Deletes all files and subdirectories under dir and dir itself.

License

Apache License

Parameter

Parameter Description
dir the directory to delete

Return

rue if all deletions were successful

Declaration

public static boolean deleteDir(File dir) 

Method Source Code

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

import java.io.*;

public class Main {
    /**//from  w w  w . j  a v a 2 s .c om
     * Deletes all files and subdirectories under dir and dir itself. Returns
     * true if all deletions were successful. If a deletion fails, the method
     * stops attempting to delete and returns false.
     *
     * @param dir the directory to delete
     * @return rue if all deletions were successful
     */
    public static boolean deleteDir(File dir) {
        boolean empty = emptyDir(dir);
        if (!empty) {
            return false;
        }
        return dir.delete();
    }

    /**
     * Deletes all files and subdirectories under dir. Returns true if all
     * deletions were successful. If a deletion fails, the method stops
     * attempting to delete and returns false.
     *
     * @param dir the directory to delete
     *
     * @return rue if all deletions were successful
     */
    public static boolean emptyDir(File dir) {
        if (dir.isDirectory()) {
            for (File child : dir.listFiles()) {
                boolean success = deleteDir(child);
                if (!success) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File dir)
  3. deleteDir(File dir)
  4. deleteDir(File dir)
  5. deleteDir(File dir)
  6. deleteDir(File dir)
  7. deleteDir(File dir)
  8. deleteDir(File dir)
  9. deleteDir(File dir)