Java Delete Folder deleteFolderRecursive(File folder)

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

Description

delete Folder Recursive

License

Open Source License

Declaration

public static int deleteFolderRecursive(File folder) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Liviu Ionescu.//from  w w  w  . j ava  2  s  . c o m
 * 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:
 *    Liviu Ionescu - initial version
 *******************************************************************************/

import java.io.File;

public class Main {
    public static int deleteFolderRecursive(File folder) {

        int count = 0;

        if (folder == null)
            return count;

        if (folder.exists()) {
            for (File f : folder.listFiles()) {
                if (f.isDirectory()) {
                    count += deleteFolderRecursive(f);
                    f.setWritable(true, false);
                    f.delete();
                } else {
                    f.setWritable(true, false);
                    f.delete();
                    count++;
                }
            }
            folder.setWritable(true, false);
            folder.delete();
        }

        return count;
    }
}

Related

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