Java Directory Delete emptyDir(File dir)

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

Description

Deletes all files and subdirectories under dir.

License

Apache License

Parameter

Parameter Description
dir the directory to delete

Return

rue if all deletions were successful

Declaration

public static boolean emptyDir(File dir) 

Method Source Code

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

import java.io.*;

public class Main {
    /**/*from   w ww . j  a v  a 2s . co m*/
     * 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;
    }

    /**
     * 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();
    }
}

Related

  1. emptyDir(File dir)
  2. emptyDir(File dir)
  3. emptyDir(File dir)
  4. emptyDir(File dir)
  5. emptyDir(File directory)
  6. emptyDir(String dirName)