Java Directory Delete emptyDirectory(File dir)

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

Description

Recursively deletes all the entries of a directory.

License

Open Source License

Parameter

Parameter Description
dir the directory to be emptied

Declaration

public static boolean emptyDirectory(File dir) 

Method Source Code


//package com.java2s;
/*//from  w ww. java2s .c o m
 *    Qizx/open 4.1
 *
 * This code is the open-source version of Qizx.
 * Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License 
 *  Version 1.1 (the "License"); you may not use this file except in 
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 * The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
 *
 */

import java.io.*;

public class Main {
    /**
     * Recursively deletes all the entries of a directory.
     * 
     * @param dirName the name of the directory to be emptied
     * @return 
     */
    public static boolean emptyDirectory(String dirName) {
        return emptyDirectory(new File(dirName));
    }

    /**
     * Recursively deletes all the entries of a directory.
     * 
     * @param dir the directory to be emptied
     */
    public static boolean emptyDirectory(File dir) {
        String[] children = dir.list();
        boolean ok = true;
        if (children != null) {
            for (int i = 0; i < children.length; ++i) {
                File child = new File(dir, children[i]);

                if (child.isDirectory()) {
                    if (!removeFile(child, /*force*/ true))
                        ok = false;
                } else if (!child.delete())
                    ok = false;
            }
        }
        return ok;
    }

    /**
     * Deletes a file or an empty directory.
     * 
     * @param fileName the name of the file or empty directory to be deleted
     * @return <code>true</code> if the file or directory has been
     * successfully deleted; <code>false</code> otherwise
     */
    public static boolean removeFile(String fileName) {
        return removeFile(fileName, /*force*/ false);
    }

    /**
     * Deletes a file or a directory, possibly emptying the directory before
     * deleting it.
     * 
     * @param fileName the name of the file or directory to be deleted
     * @param force if <code>true</code> and the file to be deleted is a
     * non-empty directory, empty it before attempting to delete it; if
     * <code>false</code>, do not empty directories
     * @return <code>true</code> if the file or directory has been
     * successfully deleted; <code>false</code> otherwise
     */
    public static boolean removeFile(String fileName, boolean force) {
        return removeFile(new File(fileName), force);
    }

    /**
     * Deletes a file or a directory, possibly emptying the directory before
     * deleting it.
     * 
     * @param file the file or directory to be deleted
     * @param force if <code>true</code> and the file to be deleted is a
     * non-empty directory, empty it before attempting to delete it; if
     * <code>false</code>, do not empty directories
     * @return <code>true</code> if the file or directory has been
     * successfully deleted; <code>false</code> otherwise
     */
    public static boolean removeFile(File file, boolean force) {
        if (file.isDirectory() && force)
            emptyDirectory(file);

        return file.delete();
    }
}

Related

  1. emptyDir(File directory)
  2. emptyDir(String dirName)
  3. emptyDir(String path)
  4. emptyDirectory(File dir)
  5. emptyDirectory(File dir)
  6. emptyDirectory(File dir)
  7. emptyDirectory(File directory)
  8. emptyDirectory(File directory)
  9. emptyDirectory(File path)