Java Delete Directory Recursively deleteDirectoryRecursively(File toDelete)

Here you can find the source of deleteDirectoryRecursively(File toDelete)

Description

Delete file or empty and nonempty directory.

License

Open Source License

Parameter

Parameter Description
toDelete file or directory to delete.

Declaration

public static void deleteDirectoryRecursively(File toDelete) 

Method Source Code

//package com.java2s;
/*//  www  .  j a v  a 2 s. co  m
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * This 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 software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;

public class Main {
    /**
     * Delete file or empty and nonempty directory.
     * 
     * @param toDelete file or directory to delete.
     */
    public static void deleteDirectoryRecursively(File toDelete) {
        if (toDelete != null) {
            if (toDelete.isDirectory()) {
                File[] sub = toDelete.listFiles();
                if (sub != null && sub.length > 0) {
                    for (File f : sub) {
                        deleteDirectoryRecursively(f);
                    }
                }
            }
            toDelete.delete();
        }
    }
}

Related

  1. deleteDirectoryRecursive(File directory)
  2. deleteDirectoryRecursive(File f)
  3. deleteDirectoryRecursively(File rootFile)
  4. deleteDirectoryRecursivelyE(File dir)
  5. deleteDirectoryRecursivly(File directory)
  6. deleteDirRecursive(File aDir)
  7. deleteDirRecursively(File dir)