Java Delete Empty Directory deleteEmptyChildren(java.io.File file)

Here you can find the source of deleteEmptyChildren(java.io.File file)

Description

delete Empty Children

License

Open Source License

Declaration

public static boolean deleteEmptyChildren(java.io.File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Red Hat, Inc./*  w  w  w. ja  v  a2  s. c o m*/
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

public class Main {
    public static boolean deleteEmptyChildren(java.io.File file) {
        boolean b = true;
        if (file.isDirectory()) {
            java.io.File[] children = file.listFiles();
            for (int i = 0; i < children.length; i++)
                b &= deleteEmptyFolders(children[i]);
        }
        return b;
    }

    public static boolean deleteEmptyFolders(java.io.File file) {
        boolean b = true;
        if (file.isDirectory()) {
            java.io.File[] children = file.listFiles();
            for (int i = 0; i < children.length; i++)
                b &= deleteEmptyFolders(children[i]);
            if (file.listFiles().length == 0)
                file.delete();
        }
        return b;
    }
}

Related

  1. deleteEmptyDir(File directory)
  2. deleteEmptyDir(File file)
  3. deleteEmptyDirectories(List dirs)
  4. deleteEmptyDirectory(File fileOrDirectory)