Java Delete Directory deleteDirectory(File dir)

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

Description

Deletes a directory with all its files and subdirectories.

License

Open Source License

Parameter

Parameter Description
dir The directory to delete.

Exception

Parameter Description
IOException Thrown if something goes wrong.

Declaration

public static void deleteDirectory(File dir) throws IOException 

Method Source Code

//package com.java2s;
/*//from   ww  w  . j a  v  a2 s .co m
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-21 15:38:33 +0100 (Sun, 21 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6835 $
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Deletes a directory with all its files and subdirectories.
     *
     * @param dir The directory to delete.
     * @throws IOException Thrown if something goes wrong.
     */
    public static void deleteDirectory(File dir) throws IOException {
        if (!dir.exists()) {
            // Nothing to do
            return;
        }

        if (!dir.isDirectory()) {
            throw new IOException("File is not a directory: " + dir.getAbsolutePath());
        }

        // Delete all the files and subdirectories
        File[] fileArr = dir.listFiles();
        if (fileArr != null) {
            for (int i = 0; i < fileArr.length; i++) {
                if (fileArr[i].isDirectory()) {
                    deleteDirectory(fileArr[i]);
                } else {
                    if (!fileArr[i].delete()) {
                        throw new IOException("Can't delete file: " + fileArr[i].getAbsolutePath());
                    }
                }
            }
        }

        // Delete the directory
        if (!dir.delete()) {
            throw new IOException("Can't delete directory: " + dir.getAbsolutePath());
        }
    }
}

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 isInitialDelete)
  8. deleteDirectory(File dir, boolean recursive)
  9. deleteDirectory(File dir, Map preserve)