Java File Path Delete deleteDir(File dir)

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

Description

Deletes the specified diretory and any files and directories in it recursively.

License

BSD License

Parameter

Parameter Description
dir The directory to remove.

Exception

Parameter Description
IOException If the directory could not be removed.

Declaration

public static void deleteDir(File dir) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   ww  w  . j  a  va 2 s. co  m*/
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Deletes the specified diretory and any files and directories in it
     * recursively.
     * 
     * @param dir
     *            The directory to remove.
     * @throws IOException
     *             If the directory could not be removed.
     */
    public static void deleteDir(File dir) throws IOException {
        if (!dir.isDirectory()) {
            throw new IOException("Not a directory " + dir);
        }

        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];

            if (file.isDirectory()) {
                deleteDir(file);
            } else {
                boolean deleted = file.delete();
                if (!deleted) {
                    throw new IOException("Unable to delete file" + file);
                }
            }
        }

        dir.delete();
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File dir)
  3. deleteDir(File dir)
  4. deleteDir(File dir)
  5. deleteDir(File dir)
  6. deleteDir(File dir)
  7. deleteDir(File dir)
  8. deleteDir(File dir)
  9. deleteDir(File dir)