Java Recursive Delete recursiveDelete(File folder)

Here you can find the source of recursiveDelete(File folder)

Description

fully delete a folder

License

Open Source License

Parameter

Parameter Description
folder a parameter

Return

whether deletion was successful

Declaration

public static boolean recursiveDelete(File folder) 

Method Source Code


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

import java.io.File;

public class Main {
    /**/*from   ww  w  . ja  va2  s .c  o m*/
     * fully delete a folder
     * 
     * @param folder
     * @return whether deletion was successful
     */
    public static boolean recursiveDelete(File folder) {
        boolean ret = _recursiveDelete(folder);
        clean();
        return ret;
    }

    private static boolean _recursiveDelete(File folder) {
        clean(); /* windows needs this */
        if (folder.isFile()) {
            return folder.delete();
        } else {
            String[] l = folder.list();
            if (l != null) {
                for (String aL : l) {
                    if (recursiveDelete(new File(folder.getPath(), aL)) == false) {
                        System.err.println("deleting " + aL + " in " + folder.getPath() + " failed!");
                        return false;
                    }
                }
            }
            clean();
            return folder.delete();
        }
    }

    private static void clean() {
        System.gc();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        System.gc();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        System.gc();
    }
}

Related

  1. recursiveDelete(File file)
  2. recursiveDelete(File file)
  3. recursiveDelete(File file, boolean deleteParentFile)
  4. recursiveDelete(File fileOrDir)
  5. recursiveDelete(File fileOrDir)
  6. recursiveDelete(File parent)
  7. recursiveDelete(File pathToFolderOrFile)
  8. recursiveDelete(File root)
  9. recursiveDelete(File root)