Java Folder Delete emptyFolder(final File folder)

Here you can find the source of emptyFolder(final File folder)

Description

Recursively deletes whole content of the given folder.

License

Open Source License

Parameter

Parameter Description
folder folder to be emptied

Return

true if given folder becomes empty or not exists

Declaration

public static boolean emptyFolder(final File folder) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Java Plug-in Framework (JPF)/*  w  w  w .java  2 s  .c o  m*/
 * Copyright (C) 2004-2006 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/

import java.io.File;

public class Main {
    /**
     * Recursively deletes whole content of the given folder.
     * @param folder folder to be emptied
     * @return <code>true</code> if given folder becomes empty or not exists
     */
    public static boolean emptyFolder(final File folder) {
        if (!folder.isDirectory()) {
            return true;
        }
        File[] files = folder.listFiles();
        boolean result = true;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                if (emptyFolder(file)) {
                    result &= file.delete();
                } else {
                    result = false;
                }
            } else {
                result &= file.delete();
            }
        }
        return result;
    }
}

Related

  1. emptyFolder(File folder)
  2. emptyFolder(File folder)
  3. emptyFolder(File folder, boolean ignoreCannotDel)
  4. emptyFolder(File toEmpty)
  5. emptyFolder(final File folder)