Java File Path Delete deleteDirectory(File path)

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

Description

Convenience function to delete a directory.

License

Open Source License

Parameter

Parameter Description
path the path to delete

Exception

Parameter Description
IOException If the operation fails.

Declaration

public static void deleteDirectory(File path) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

public class Main {
    /**//from w w  w  .  jav a  2s .c o  m
     * Convenience function to delete a directory.
     * 
     * @param path the path to delete
     * @throws IOException If the operation fails.
     */
    public static void deleteDirectory(File path) throws IOException {
        if (!path.exists())
            return;

        final File[] files = path.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory())
                    deleteDirectory(file);
                else if (!file.delete()) {
                    throw new IOException(String.format("Unable to delete file '%s'", file));
                }
            }
        }

        if (!path.delete())
            throw new IOException(String.format("Unable to delete directory '%s'", path));
    }
}

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)