Java Delete File Recursively deleteRecursive(File file)

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

Description

Recursively deletes a file/folder and all its subcontent.

License

Open Source License

Parameter

Parameter Description
file a parameter

Declaration

public static void deleteRecursive(File file) 

Method Source Code

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

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from  ww w.  j  a  v a2 s . c o m
     * Recursively deletes a file/folder and all its subcontent.
     * In other words, "rm -rf"
     * @param file
     */
    public static void deleteRecursive(File file) {

        List<File> exploreQueue = new ArrayList<File>();
        List<File> exploreQueue2 = new ArrayList<File>();
        List<File> deleteQueue = new ArrayList<File>();
        exploreQueue.add(file);

        while (!exploreQueue.isEmpty()) {
            deleteQueue.addAll(0, exploreQueue);
            for (File toExplore : exploreQueue) {
                if (toExplore.isDirectory()) {
                    for (File child : toExplore.listFiles()) {
                        exploreQueue2.add(0, child);
                    }
                }
            }
            exploreQueue.clear();
            exploreQueue.addAll(exploreQueue2);
            exploreQueue2.clear();
        }

        for (File toDelete : deleteQueue) {
            toDelete.delete();
        }

    }
}

Related

  1. deleteRecursive(File f)
  2. deleteRecursive(File f)
  3. deleteRecursive(File file)
  4. deleteRecursive(File file)
  5. deleteRecursive(File file)
  6. deleteRecursive(File file)
  7. deleteRecursive(File file)
  8. deleteRecursive(File file)
  9. deleteRecursive(File file)