Android Directory Remove Recursive recursiveRemoveDir(File directory)

Here you can find the source of recursiveRemoveDir(File directory)

Description

This class del a directory recursively,that means delete all files and directorys.

Parameter

Parameter Description
File directorythe directory that will be deleted.

Declaration

public static void recursiveRemoveDir(File directory) throws Exception 

Method Source Code

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

public class Main {
    /**//from  ww  w.  j a v a2s . c  o m
     *  This class del a directory recursively,that means delete all files and directorys.
     *
     *  @param File directory      the directory that will be deleted.
     */
    public static void recursiveRemoveDir(File directory) throws Exception {
        if (!directory.exists())
            throw new IOException(directory.toString() + " do not exist!");

        String[] filelist = directory.list();
        File tmpFile = null;
        for (int i = 0; i < filelist.length; i++) {
            tmpFile = new File(directory.getAbsolutePath(), filelist[i]);
            if (tmpFile.isDirectory()) {
                recursiveRemoveDir(tmpFile);
            } else if (tmpFile.isFile()) {
                try {
                    tmpFile.delete();
                } catch (Exception ex) {
                    throw new Exception(tmpFile.toString()
                            + " can not be deleted " + ex.getMessage());
                }
            }
        }
        try {
            directory.delete();
        } catch (Exception ex) {
            throw new Exception(directory.toString()
                    + " can not be deleted " + ex.getMessage());
        } finally {
            filelist = null;
        }
    }
}

Related

  1. deltree(File directory)
  2. deltree(String directory)
  3. removeDirectories(File file)
  4. removeDirectories(File file, String regex)
  5. delete(String root)
  6. DeleteRecursive(File fileOrDirectory)