Java Recursive Delete recursiveDelete(File file)

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

Description

Recursively deletes a file/folder structure.

License

Open Source License

Parameter

Parameter Description
file a parameter

Declaration

public static boolean recursiveDelete(File file) 

Method Source Code


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

import java.io.*;

public class Main {
    /**/*from   ww  w  .  j  av a2  s  . c o  m*/
     * Recursively deletes a file/folder structure. True is returned if ALL
     * files were deleted. If it returns false, some or none of the files
     * may have been deleted.
     *
     * @param file
     * @return
     */
    public static boolean recursiveDelete(File file) {
        //Hopefully this works around JVM bugs.
        //It seems that on windows machines, until garbage
        //collection happens, the system will still
        //have file locks on the file, even if the Streams
        //were properly closed.
        System.gc();
        if (file.isDirectory()) {
            boolean ret = true;
            for (File f : file.listFiles()) {
                if (!recursiveDelete(f)) {
                    ret = false;
                }
            }
            if (!file.delete()) {
                ret = false;
            }
            return ret;
        } else {
            return file.delete();
        }
    }
}

Related

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