Java Utililty Methods File Path Delete

List of utility methods to do File Path Delete

Description

The list of methods to do File Path Delete are organized into topic(s).

Method

booleandeleteDir(File path)
Deletes a given File , if it exists.
if (path == null) {
    throw new NullPointerException("Path cannot be null!");
if (!path.exists()) {
    throw new FileNotFoundException("File not found: " + path);
boolean result = true;
if (path.isDirectory()) {
...
booleandeleteDir(File path)
delete Dir
if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            deleteDir(files[i]);
        } else {
            files[i].delete();
boolean ok = path.delete();
if (!ok)
    System.err.println("[WARN] Unable to delete: " + path);
return ok;
voiddeleteDir(final String path)
delete Dir
File file = new File(path);
deleteDir(file);
booleandeleteDir(String path)
Deletes a file or directory.
return deleteDir(new File(path));
booleandeleteDir(String path)
delete Dir
File dir = new File(path);
return deleteDir(dir);
intdeleteDirByDoc(String path)
delete Dir By Doc
path = path.replaceAll("/", "\\\\");
path = path.replaceAll("\\\\\\\\", "\\\\");
if (new File(path).exists()) {
    Runtime runtime = Runtime.getRuntime();
    try {
        runtime.exec("cmd /c rd /s/q " + path);
        return 0;
    } catch (IOException e) {
...
booleandeleteDirectory(File directoryPath)
Utility method for deleting a folder.
boolean deleated = true;
if (directoryPath.exists() && directoryPath.isDirectory()) {
    if (directoryPath.list().length > 0) {
        for (File f : directoryPath.listFiles()) {
            if (f.isFile()) {
                if (!f.delete()) {
                    deleated = false;
            } else {
                if (!deleteDirectory(f)) {
                    deleated = false;
        if (!directoryPath.delete()) {
            deleated = false;
    } else {
        if (!directoryPath.delete()) {
            deleated = false;
return deleated;
booleandeleteDirectory(File path)
delete Directory
if (path.exists()) {
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            deleteDirectory(files[i]);
        } else {
            System.out.println("file deleted: " + files[i].getPath());
            files[i].delete();
...
voiddeleteDirectory(File path)
delete Directory
if (path != null && path.exists()) {
    if (path.isDirectory()) {
        for (File f : path.listFiles()) {
            deleteDirectory(f);
    path.delete();
booleandeleteDirectory(File path)
Tries to delete a directory and all contained files and subdirectories
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]);
...