Java Delete Tree deleteTree(File f)

Here you can find the source of deleteTree(File f)

Description

Recursively delete the contents of a directory, including any subdirectories.

License

Open Source License

Parameter

Parameter Description
f is either a normal file (which will be deleted) or a directory (which will be emptied and then deleted).

Declaration

public static void deleteTree(File f) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2015 BEA Systems, Inc.
 * 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://w  ww  .  j  a  v  a2 s .co  m
 *    wharley@bea.com - initial API and implementation
 *    IBM Corporation - fix for 342936
 *    het@google.com - Bug 415274 - Annotation processing throws a NPE in getElementsAnnotatedWith()
 *******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Recursively delete the contents of a directory, including any subdirectories.
     * This is not optimized to handle very large or deep directory trees efficiently.
     * @param f is either a normal file (which will be deleted) or a directory
     * (which will be emptied and then deleted).
     */
    public static void deleteTree(File f) {
        if (null == f) {
            return;
        }
        File[] children = f.listFiles();
        if (null != children) {
            // if f has any children, (recursively) delete them
            for (File child : children) {
                deleteTree(child);
            }
        }
        // At this point f is either a normal file or an empty directory
        f.delete();
    }
}

Related

  1. deleteTree(File dir)
  2. deleteTree(File dir)
  3. deleteTree(File file, boolean check)
  4. deleteTree(final File file)
  5. delTree(File dir)
  6. delTree(File dir)