Java Recursive Delete recursiveDelete(String p_path, boolean p_deletemetoo)

Here you can find the source of recursiveDelete(String p_path, boolean p_deletemetoo)

Description

Recursively delete a directory.

License

Open Source License

Parameter

Parameter Description
p_path String
p_deletemetoo boolean

Return

boolean

Declaration

public static boolean recursiveDelete(String p_path, boolean p_deletemetoo) 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**// w  w w  .j a  va2 s .c  o  m
     * Recursively delete a directory.  BE CAREFUL, this deletes everything
     * @param p_path String
     * @param p_deletemetoo boolean
     * @return boolean
     */
    public static boolean recursiveDelete(String p_path, boolean p_deletemetoo) {
        if (p_path == null) {
            return true;
        }
        String path = p_path;
        if (path.endsWith("\\") || path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        File f = new File(path);
        if (f.isFile()) {
            return p_deletemetoo ? f.delete() : true;
        }
        String contents[] = f.list();
        int len = contents != null ? contents.length : 0;
        boolean result = true;
        for (int i = 0; i < len; i++) {
            if (!recursiveDelete(path + File.separator + contents[i], true)) {
                result = false;
            }
        }

        if (p_deletemetoo && !f.delete()) {
            result = false;
        }
        return result;
    }

    /**
     * Is the given name a file ?
     * @param p_path String
     * @return boolean
     */
    public static boolean isFile(String p_path) {
        File f = new File(p_path);
        return f.isFile();
    }
}

Related

  1. recursiveDelete(final File path)
  2. recursiveDelete(final File path, final boolean deleteParent)
  3. recursiveDelete(final File pFile)
  4. recursiveDelete(final File root, final File file)
  5. recursiveDelete(String fileName)
  6. recursiveDelete(String path)
  7. recursiveDeleteFile(File f)
  8. recursiveDeleteFile(File file)
  9. recursiveDeleteFile(File file)