Java Delete Empty Directory deleteEmptyParentFolders(File leafFolder)

Here you can find the source of deleteEmptyParentFolders(File leafFolder)

Description

Delete empty parent folders.

License

Open Source License

Parameter

Parameter Description
leafFolder the leaf folder

Declaration

public static void deleteEmptyParentFolders(File leafFolder) 

Method Source Code

//package com.java2s;
/***********************************************************************************************************************
 * Copyright (c) 2008 empolis GmbH and brox IT Solutions GmbH. All rights reserved. This program and the accompanying
 * materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Marius Cimpean (brox IT Solutions GmbH) - initial creator
 **********************************************************************************************************************/

import java.io.File;

public class Main {
    /** Binary Storage Service configured root location */
    private static File _root = null;

    /**/*from  w  w  w  .  j av  a2 s. c om*/
     * Delete empty parent folders.
     * 
     * @param leafFolder
     *          the leaf folder
     */
    public static void deleteEmptyParentFolders(File leafFolder) {
        while (leafFolder != null && !leafFolder.equals(_root)) {
            final String[] elements = leafFolder.list();
            if (elements == null || elements.length > 0) {
                // folder does not exist anymore or is not empty.
                return;
            }
            leafFolder.delete();
            leafFolder = leafFolder.getParentFile();
        }
    }
}

Related

  1. deleteEmptyDirs(File dir)
  2. deleteEmptyDirs(File dir)
  3. deleteEmptyFiles(String p)
  4. deleteEmptyFolders(java.io.File file)
  5. deleteEmptyFolders(String[] args)
  6. deleteEmptyParents(File currentParentFile, String inputDir)