Java File Path Delete deleteDirectory(File path)

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

Description

Tries to delete a directory and all contained files and subdirectories

License

LGPL

Parameter

Parameter Description
path File or Directory to delete.

Return

returns true if the directory could be deleted. Also return true if the directory did not exist from the start.

Declaration

static public boolean deleteDirectory(File path) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;

public class Main {
    /**//ww  w . j  a  v  a2  s  . co  m
     * Tries to delete a directory and all contained files and subdirectories
     *
     * @param path
     *            File or Directory to delete.
     * @return returns true if the directory could be deleted. Also return true
     *         if the directory did not exist from the start.
     */
    static public boolean deleteDirectory(File path) {
        if (!path.exists()) {
            return true;
        }
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }
}

Related

  1. deleteDir(String path)
  2. deleteDirByDoc(String path)
  3. deleteDirectory(File directoryPath)
  4. deleteDirectory(File path)
  5. deleteDirectory(File path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path)
  8. deleteDirectory(File path)
  9. deleteDirectory(File path)