Java Delete Folder deleteFolder(String folder)

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

Description

Deletes a folder from the file system.

License

Open Source License

Parameter

Parameter Description
filename the pathname of the folder to erase.

Return

true if and only if the folder is successfully deleted; false otherwise.

Declaration

public static boolean deleteFolder(String folder) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013-2015 UAH Space Research Group.
 * 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:/*  ww  w  .j ava 2s  .c o m*/
 *     MICOBS SRG Team - Initial API and implementation
 ******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Deletes a folder from the file system. The method recursively
     * deletes the contents of the folder and then the folder itself.
     * 
     * @param filename the pathname of the folder to erase.
     * @return <code>true</code> if and only if the folder is
     * successfully deleted; <code>false</code> otherwise.
     */
    public static boolean deleteFolder(String folder) {

        File folder2Delete = new File(folder);

        if (folder2Delete.exists()) {
            File[] files = folder2Delete.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteFolder(files[i].getPath());
                } else {
                    files[i].delete();
                }
            }
        }
        return (folder2Delete.delete());
    }
}

Related

  1. deleteFolder(File folder)
  2. deleteFolder(final File folder)
  3. deleteFolder(final File folder)
  4. deleteFolder(final String folder)
  5. deleteFolder(String folder)
  6. deleteFolder(String name)
  7. deleteFolderContents(File folder)
  8. deleteFolderContents(File folder)
  9. deleteFolderContents(File folder)