Java Delete Empty Directory deleteEmptyDir(File file)

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

Description

delete Empty Dir

License

Open Source License

Declaration

public static void deleteEmptyDir(File file) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.util.List;

public class Main {

    public static void deleteEmptyDir(File file) {
        if (!file.exists() || file.isFile()) {
            return;
        }//from  w w w .java2s. c  o  m
        if (file.isDirectory()) {
            if (file.list().length == 0) {
                file.delete();
            } else {
                for (File f : file.listFiles()) {
                    deleteEmptyDir(f);
                }
                if (file.list().length == 0) {
                    file.delete();
                }
            }
        }
    }

    public static void deleteEmptyDir(File file, List<File> deleteList) {
        if (!file.exists() || file.isFile()) {
            return;
        }
        if (file.isDirectory()) {
            if (file.list().length == 0) {
                deleteList.add(file);
                file.delete();
            } else {
                for (File f : file.listFiles()) {
                    deleteEmptyDir(f, deleteList);
                }
                if (file.list().length == 0) {
                    deleteList.add(file);
                    file.delete();
                }
            }
        }
    }
}

Related

  1. deleteEmptyChildren(java.io.File file)
  2. deleteEmptyDir(File directory)
  3. deleteEmptyDirectories(List dirs)
  4. deleteEmptyDirectory(File fileOrDirectory)
  5. deleteEmptyDirectoryRecursive(File directory)
  6. deleteEmptyDirectoryRecursive(File directory)