Java Delete Empty Directory deleteEmptyDirectoryRecursive(File directory)

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

Description

Deletes an empty directory, including all parent directories that are also empty, stopping at the first non-empty parent.

License

LGPL

Parameter

Parameter Description
directory The start directory

Exception

Parameter Description
IOException In case of an I/O error

Declaration

public static void deleteEmptyDirectoryRecursive(File directory) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  ww  w  .  j  a  v a2s  .c o m*/
 * Copyright 2011-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Deletes an empty directory, including all parent directories that are
     * also empty, stopping at the first non-empty parent.
     * 
     * @param directory
     *        The start directory
     * @throws IOException
     *         In case of an I/O error
     */
    public static void deleteEmptyDirectoryRecursive(File directory) throws IOException {
        if ((directory != null) && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if ((files == null) || (files.length == 0)) {
                if (!directory.delete())
                    throw new IOException("Could not delete empty directory: " + directory);
                deleteEmptyDirectoryRecursive(directory.getParentFile());
            }
        }
    }
}

Related

  1. deleteEmptyDir(File directory)
  2. deleteEmptyDir(File file)
  3. deleteEmptyDirectories(List dirs)
  4. deleteEmptyDirectory(File fileOrDirectory)
  5. deleteEmptyDirectoryRecursive(File directory)
  6. deleteEmptyDirs(File dir)
  7. deleteEmptyDirs(File dir)
  8. deleteEmptyFiles(String p)
  9. deleteEmptyFolders(java.io.File file)