Java Delete File Recursively deleteRecursively(File file)

Here you can find the source of deleteRecursively(File file)

Description

Deletes the given file or directory.

License

Open Source License

Parameter

Parameter Description
file the file or directory to be deleted

Return

true iff deletion fully succeeded

Declaration

@SuppressWarnings("ConstantConditions")
public static boolean deleteRecursively(File file) 

Method Source Code


//package com.java2s;
/*//from  ww w  .  j  av  a 2 s . c om
 * Copyright (c) 2015 the original author or authors.
 * 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:
 *     Etienne Studer & Don?t Csik?s (Gradle Inc.) - initial API and implementation and initial documentation
 */

import java.io.File;

public class Main {
    /**
     * Deletes the given file or directory. In case of a directory, all its content is deleted recursively.
     *
     * @param file the file or directory to be deleted
     * @return <code>true</code> iff deletion fully succeeded
     */
    @SuppressWarnings("ConstantConditions")
    public static boolean deleteRecursively(File file) {
        if (file.isDirectory()) {
            boolean success = true;
            File[] children = file.listFiles();
            for (File child : children) {
                success &= deleteRecursively(child);
            }
            return success && file.delete();
        } else {
            return file.delete();
        }
    }
}

Related

  1. deleteRecursively(File file)
  2. deleteRecursively(File file)
  3. deleteRecursively(File file)
  4. deleteRecursively(File file)
  5. deleteRecursively(File file)
  6. deleteRecursively(File file)
  7. deleteRecursively(File file)
  8. deleteRecursively(File file)
  9. deleteRecursively(File fileEntry)