Java Delete Folder deleteFolderRecursively(File folder)

Here you can find the source of deleteFolderRecursively(File folder)

Description

delete Folder Recursively

License

Open Source License

Declaration

public static void deleteFolderRecursively(File folder) 

Method Source Code


//package com.java2s;
/*/*from  w w  w. j a va 2 s.  c  o  m*/
 * Created by Angel Leon (@gubatron), Alden Torres (aldenml)
 * Copyright (c) 2011-2015, FrostWire(R). All rights reserved.
 *
 * This program 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 deleteFolderRecursively(File folder) {
        if (folder != null && folder.isDirectory() && folder.canWrite()) {
            //delete your contents and recursively delete sub-folders
            File[] listFiles = folder.listFiles();

            if (listFiles != null) {
                for (File f : listFiles) {
                    if (f.isFile()) {
                        f.delete();
                    } else if (f.isDirectory()) {
                        deleteFolderRecursively(f);
                    }
                }
                folder.delete();
            }
        }
    }
}

Related

  1. deleteFolderContents(File folder)
  2. deleteFolderIfEmpty(File f)
  3. deleteFolderRec(File dir)
  4. deleteFolderRecursive(File folder)
  5. deleteFolderRecursively(File file)