Java Delete Directory deleteDirectory(File directory)

Here you can find the source of deleteDirectory(File directory)

Description

Delete directory.

License

Mozilla Public License

Parameter

Parameter Description
directory the directory

Return

true, if successful

Declaration

public static boolean deleteDirectory(File directory) 

Method Source Code


//package com.java2s;
/* SpagoBI, the Open Source Business Intelligence suite
    /*www.ja va2s  . c  o  m*/
 * Copyright (C) 2012 Engineering Ingegneria Informatica S.p.A. - SpagoBI Competency Center
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0, without the "Incompatible With Secondary Licenses" notice. 
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import java.io.File;

public class Main {
    /**
     * Delete directory.
     * 
     * @param directory the directory
     * 
     * @return true, if successful
     */
    public static boolean deleteDirectory(File directory) {
        try {
            if (directory.isDirectory()) {
                File[] files = directory.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    if (file.isFile()) {
                        boolean deletion = file.delete();
                        if (!deletion)
                            return false;
                    } else
                        deleteDirectory(file);
                }
            }
            boolean deletion = directory.delete();
            if (!deletion)
                return false;
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

Related

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