Java Recursive Delete recursiveDelete(File rootDir, boolean deleteRoot)

Here you can find the source of recursiveDelete(File rootDir, boolean deleteRoot)

Description

recursive Delete

License

Open Source License

Declaration

public static void recursiveDelete(File rootDir, boolean deleteRoot) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Weasis Team and others.
 * 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:/* ww w. ja va2s  .  co  m*/
 *     Nicolas Roduit - initial API and implementation
 *******************************************************************************/

import java.io.File;

public class Main {
    public static void recursiveDelete(File rootDir, boolean deleteRoot) {
        if ((rootDir == null) || !rootDir.isDirectory()) {
            return;
        }
        File[] childDirs = rootDir.listFiles();
        if (childDirs != null) {
            for (File f : childDirs) {
                if (f.isDirectory()) {
                    // deleteRoot used only for the first level, directory is deleted in next line
                    recursiveDelete(f, false);
                    deleteFile(f);
                } else {
                    deleteFile(f);
                }
            }
        }
        if (deleteRoot) {
            rootDir.delete();
        }
    }

    private static void deleteFile(File fileOrDirectory) {
        try {
            fileOrDirectory.delete();
        } catch (Exception e) {
            // Do nothing, wait next start to delete it
        }
    }
}

Related

  1. recursiveDelete(File folder)
  2. recursiveDelete(File parent)
  3. recursiveDelete(File pathToFolderOrFile)
  4. recursiveDelete(File root)
  5. recursiveDelete(File root)
  6. recursiveDelete(File target)
  7. recursiveDelete(final File directory)
  8. recursiveDelete(final File file, final boolean childrenOnly)
  9. recursiveDelete(final File fileOrDir)