Java Delete Folder deleteFolder(File folder)

Here you can find the source of deleteFolder(File folder)

Description

delete Folder

License

Open Source License

Declaration

public static boolean deleteFolder(File folder) 

Method Source Code


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

public class Main {
    public static boolean deleteFolder(File folder) {
        boolean result = false;
        try {//from  w  w w. j a  v  a  2s.  c  o m
            String childs[] = folder.list();
            if (childs == null || childs.length <= 0) {
                if (folder.delete()) {
                    result = true;
                }
            } else {
                for (int i = 0; i < childs.length; i++) {
                    String childName = childs[i];
                    String childPath = folder.getPath() + File.separator + childName;
                    File filePath = new File(childPath);
                    if (filePath.exists() && filePath.isFile()) {
                        if (filePath.delete()) {
                            result = true;
                        } else {
                            result = false;
                            break;
                        }
                    } else if (filePath.exists() && filePath.isDirectory()) {
                        if (deleteFolder(filePath)) {
                            result = true;
                        } else {
                            result = false;
                            break;
                        }
                    }
                }
            }
            folder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }
}

Related

  1. deleteFolder(File file, boolean withCurrentFolder)
  2. deleteFolder(File folder)
  3. deleteFolder(File folder)
  4. deleteFolder(File folder)
  5. deleteFolder(File folder)
  6. deleteFolder(File folder)
  7. deleteFolder(File folder)
  8. deleteFolder(File folder)
  9. deleteFolder(final File folder)