Here you can find the source of recursiveDelete(File rootDir, boolean deleteRoot)
public static void recursiveDelete(File rootDir, boolean deleteRoot)
//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 } } }