Java Delete Directory deleteDirectory(String dirname, boolean recursive)

Here you can find the source of deleteDirectory(String dirname, boolean recursive)

Description

INTERNAL: Deletes the directory with the given name, recursively if specified.

License

Apache License

Declaration

public static void deleteDirectory(String dirname, boolean recursive) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  ww  . java  2  s . co  m*/
 * #!
 * Ontopia Engine
 * #-
 * Copyright (C) 2001 - 2013 The Ontopia Project
 * #-
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * !#
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * INTERNAL: Deletes the directory with the given name, recursively
     * if specified.
     */
    public static void deleteDirectory(String dirname, boolean recursive) throws IOException {
        deleteDirectory(new File(dirname), recursive);
    }

    /**
     * INTERNAL: Deletes the directory, recursively if specified.
     */
    public static void deleteDirectory(File dir, boolean recursive) throws IOException {
        // Check to see if directory exists.
        if (!dir.exists())
            throw new IOException("The directory '" + dir + "' does not exist.");
        // Complain if file is not a directory
        if (!dir.isDirectory())
            throw new IOException("'" + dir + "' is not a directory.");

        if (recursive) {
            // Get a list of the files in the directory
            File[] dirfiles = dir.listFiles();
            // Note: listFiles() returns null if not directory or exception occurred.
            if (dirfiles != null) {
                for (int i = 0; i < dirfiles.length; i++) {
                    // Delete file or directory recursively
                    delete(dirfiles[i], true);
                }
            }
        }

        // Delete the directory (since it should now be empty)
        if (!dir.delete())
            throw new IOException("Couldn't delete directory '" + dir + "'.");
    }

    /**
     * INTERNAL: Deletes the file or directory with the given name,
     * recursively if specified.
     */
    public static void delete(String file_or_dirname, boolean recursive) throws IOException {
        delete(new File(file_or_dirname), recursive);
    }

    /**
     * INTERNAL: Deletes the file or directory, recursively if
     * specified.
     */
    public static void delete(File file_or_dir, boolean recursive) throws IOException {
        // Check to see if file exists.
        if (!file_or_dir.exists())
            throw new IOException("The File '" + file_or_dir + "' does not exist.");

        if (recursive) {
            // Get a list of the files in the directory
            File[] dirfiles = file_or_dir.listFiles();
            // Note: listFiles() returns null if not directory or exception occurred.
            if (dirfiles != null) {
                for (int i = 0; i < dirfiles.length; i++) {
                    // Delete file or directory recursively
                    delete(dirfiles[i], true);
                }
            }
        }

        // Delete the file (if it is a directory it should now be empty)
        if (!file_or_dir.delete())
            throw new IOException("Couldn't delete File '" + file_or_dir + "'.");
    }
}

Related

  1. deleteDirectory(String directory)
  2. deleteDirectory(String directoryName)
  3. deleteDirectory(String directoryName)
  4. deleteDirectory(String dirName)
  5. deleteDirectory(String dirName)
  6. deleteDirectory(String fileName)
  7. deleteDirectory(String folder)
  8. deleteDirectory(String name)
  9. deleteDirectoryContent(final String strDir)