Here you can find the source of deleteRecursively(File root, boolean deleteRoot)
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. |
true
if the File
was deleted, otherwise false
public static boolean deleteRecursively(File root, boolean deleteRoot)
//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; } }