delete folder recursively - Android java.io

Android examples for java.io:Folder

Description

delete folder recursively

Demo Code

import android.app.Activity;
import android.text.TextUtils;
import java.io.File;

public class Main{

    public static boolean delete(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                return recursiveDelete(file);
            } else {
                return file.delete();
            }//w ww.j  av a 2  s .  c  om
        } else {
            return true;
        }
    }
    public static boolean delete(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                return recursiveDelete(file);
            } else {
                return file.delete();
            }
        } else {
            return true;
        }
    }
    private static boolean recursiveDelete(File file) {
        // Recursively delete all contents.
        File[] files = file.listFiles();

        if (files != null) {
            for (int x = 0; x < files.length; x++) {
                File childFile = files[x];
                if (childFile.isDirectory()) {
                    if (!recursiveDelete(childFile)) {
                        return false;
                    }
                } else {
                    if (!childFile.delete()) {
                        return false;
                    }
                }
            }
        }

        if (!file.delete()) {
            return false;
        }

        return true;
    }

}

Related Tutorials