Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.text.TextUtils;

import java.io.File;

public class Main {

    public static boolean deleteFile(String path) {
        boolean result = false;
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (file.exists()) {
                if (file.isFile()) {
                    result = file.delete();
                } else {
                    result = removeDir(file);
                }
            }
        }
        return result;
    }

    public static boolean removeDir(File dir) {
        boolean flag = false;
        if (dir.exists() && dir.isDirectory()) {
            File[] childFiles = dir.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                flag = dir.delete();
            } else {
                for (File child : childFiles) {
                    boolean del = false;
                    if (child.isDirectory()) {
                        del = removeDir(child);
                    } else if (child.isFile()) {
                        del = child.delete();
                    }
                    if (!del) {
                        break;
                    }
                }
                flag = dir.delete();
            }
        }
        return flag;
    }
}