Here you can find the source of recursiveDelete(final File file, final boolean childrenOnly)
Parameter | Description |
---|---|
file | the file to delete, or if this is a directory, the directory that serves as the root of a recursive deletion |
childrenOnly | if <code>true</code>, only the children of a directory are recursively deleted but the specified directory itself is spared; if <code>false</code>, the specified directory is also deleted; ignored if <code>file</code> is not a directory |
public static void recursiveDelete(final File file, final boolean childrenOnly)
//package com.java2s; /*/*from w w w . java 2s.c om*/ * Copyright 2008 Google Inc. * * Licensed 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.File; import java.io.FileFilter; public class Main { /** * Deletes a file or recursively deletes a directory. * * @param file the file to delete, or if this is a directory, the directory that serves as the * root of a recursive deletion * @param childrenOnly if <code>true</code>, only the children of a directory are recursively * deleted but the specified directory itself is spared; if <code>false</code>, the * specified directory is also deleted; ignored if <code>file</code> is not a directory */ public static void recursiveDelete(final File file, final boolean childrenOnly) { recursiveDelete(file, childrenOnly, null); } /** * Selectively deletes a file or recursively deletes a directory. Note that it is possible that * files remain if file.delete() fails. * * @param file the file to delete, or if this is a directory, the directory that serves as the * root of a recursive deletion * @param childrenOnly if <code>true</code>, only the children of a directory are recursively * deleted but the specified directory itself is spared; if <code>false</code>, the * specified directory is also deleted; ignored if <code>file</code> is not a directory * @param filter only files matching this filter will be deleted */ public static void recursiveDelete(final File file, final boolean childrenOnly, final FileFilter filter) { if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null) { for (int i = 0; i < children.length; i++) { recursiveDelete(children[i], false, filter); } } if (childrenOnly) { // Do not delete the specified directory itself. return; } } if (filter == null || filter.accept(file)) { file.delete(); } } }