Java Delete File Recursively deleteRecursively(File root, boolean deleteRoot)

Here you can find the source of deleteRecursively(File root, boolean deleteRoot)

Description

Delete the supplied java.io.File - for directories, recursively delete any nested directories or files as well.

License

Apache License

Parameter

Parameter Description
root the root <code>File</code> to delete
deleteRoot whether or not to delete the root itself or just the content of the root.

Return

true if the File was deleted, otherwise false

Declaration

public static boolean deleteRecursively(File root, boolean deleteRoot) 

Method Source Code


//package com.java2s;
/*//from  w w w.  jav a 2  s  .c  o  m
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.*;

public class Main {
    public static boolean deleteRecursively(File root) {
        return deleteRecursively(root, true);
    }

    /**
     * Delete the supplied {@link java.io.File} - for directories,
     * recursively delete any nested directories or files as well.
     *
     * @param root       the root <code>File</code> to delete
     * @param deleteRoot whether or not to delete the root itself or just the content of the root.
     * @return <code>true</code> if the <code>File</code> was deleted,
     *         otherwise <code>false</code>
     */
    public static boolean deleteRecursively(File root, boolean deleteRoot) {
        if (root != null && root.exists()) {
            if (root.isDirectory()) {
                File[] children = root.listFiles();
                if (children != null) {
                    for (File aChildren : children) {
                        deleteRecursively(aChildren);
                    }
                }
            }

            if (deleteRoot) {
                return root.delete();
            } else {
                return true;
            }
        }
        return false;
    }
}

Related

  1. deleteRecursively(File fileToDelete)
  2. deleteRecursively(File fRoot)
  3. deleteRecursively(File name)
  4. deleteRecursively(File root)
  5. deleteRecursively(File root)
  6. deleteRecursively(File[] roots)
  7. deleteRecursively(final File directory)
  8. deleteRecursively(final File directory)
  9. deleteRecursively(final File iRootFile)