Java File Path Delete deleteFolder(String folderPath)

Here you can find the source of deleteFolder(String folderPath)

Description

delete Folder

License

Open Source License

Declaration

public static void deleteFolder(String folderPath) 

Method Source Code

//package com.java2s;
/*/*from w w  w. ja v  a  2 s  .com*/
 *   This is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

import java.io.File;

public class Main {
    public static void deleteFolder(String folderPath) {
        deleteFolder(folderPath, false);
    }

    /**
     * Deletes a folder's contents
     * 
     * @param folderPath   the path to the folder to delete
     * @param keepRootFolder   if true, only the contents will be deleted, false will delete the folder itself as well
     */
    public static void deleteFolder(String folderPath, Boolean keepRootFolder) {

        File folderPathFiles = new File(folderPath);
        deleteFolder(folderPathFiles, keepRootFolder);

    }

    /**
     * Deletes a folder's contents
     * 
     * @param folder         the java.io.File folder to delete
     * @param keepRootFolder   if true, only the contents will be deleted, false will delete the folder itself as well
     */
    public static void deleteFolder(File folder, Boolean keepRootFolder) {

        if (!folder.isDirectory()) {
            return;
        }

        File[] files = folder.listFiles();

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                deleteFolder(files[i], false);
            } else {
                files[i].delete();
            }
        }

        if (!keepRootFolder) {
            folder.delete();
        }

    }
}

Related

  1. deleteFileWithoutException(final String path)
  2. deleteFileWithSuffix(String path, String suffix)
  3. deleteFolder(File path)
  4. deleteFolder(File path)
  5. deleteFolder(String filePath)
  6. deleteFolder(String folderPath)
  7. deleteFolder(String folderPath)
  8. deleteFolder(String path)
  9. deleteFolder(String path)