Java File Path Delete deleteDirectory(File path)

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

Description

delete Directory

License

Apache License

Declaration

public static void deleteDirectory(File path) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 DANS - Data Archiving and Networked Services
 *
 * 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
 *  /*from   w  w  w .ja v a  2s .co  m*/
 * 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 {
    public static void deleteDirectory(File path) throws IOException {
        if (!path.isDirectory())
            throw new IOException("Directory '" + path + "' does not exist or is not a directory.");

        if (path.isDirectory()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    if (!files[i].delete())
                        throw new IOException("Error while deleting file " + files[i].getAbsolutePath());
                }
            }
        }

        if (!path.delete())
            throw new IOException("Error while deleting directory " + path.getAbsolutePath());
    }
}

Related

  1. deleteDirectory(File path)
  2. deleteDirectory(File path)
  3. deleteDirectory(File path)
  4. deleteDirectory(File path)
  5. deleteDirectory(File path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path)
  8. deleteDirectory(File path)
  9. deleteDirectory(File path)