Java Delete Directory deleteDirectory(File dir, boolean isInitialDelete)

Here you can find the source of deleteDirectory(File dir, boolean isInitialDelete)

Description

Utility method for delete directory

License

Open Source License

Parameter

Parameter Description
dir - directory to delete
isInitialDelete - determine if the deleting process running at startup or on destroy of application

Return

true if directory succesfully deleted

Declaration

public static boolean deleteDirectory(File dir, boolean isInitialDelete) 

Method Source Code

//package com.java2s;
/**/*from   w ww  .  j  av  a  2 s . c  om*/
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * Copyright (C) 2007 Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

import java.io.*;

public class Main {
    /**
     * Utility method for delete directory
     * @param dir - directory to delete
     * @param isInitialDelete - determine if the deleting process running at startup or on destroy of application
     * @return true if directory succesfully deleted
     */
    public static boolean deleteDirectory(File dir, boolean isInitialDelete) {
        if (dir.isDirectory()) {
            if (dir.exists()) {
                for (File child : dir.listFiles()) {
                    try {
                        deleteDirectory(child, isInitialDelete);
                    } catch (Exception e) {
                        if (isInitialDelete) {
                            continue;
                        } else
                            return false;
                    }
                }
            }

        } else {
            if (dir.exists()) {
                final boolean isFileDeleted = dir.delete();
                System.out.println((isFileDeleted ? "OK     " : "ERROR ") + "Delete file '" + dir.getPath() + '\'');
            }
        }
        dir.delete();
        return true;
    }
}

Related

  1. deleteDirectory(File dir)
  2. deleteDirectory(File dir)
  3. deleteDirectory(File dir)
  4. deleteDirectory(File dir)
  5. deleteDirectory(File dir)
  6. deleteDirectory(File dir, boolean isInitialDelete)
  7. deleteDirectory(File dir, boolean recursive)
  8. deleteDirectory(File dir, Map preserve)
  9. deleteDirectory(File directory)