Here you can find the source of recursiveDeleteHelper(File parent, List
private static void recursiveDeleteHelper(File parent, List<File> failed)
//package com.java2s; /*/* w w w.j ava 2 s . c o m*/ * $Id$ * * Copyright (c) 2008 by Joel Uckelman * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License (LGPL) as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, copies are available * at http://www.opensource.org. */ import java.io.File; import java.io.IOException; import java.util.List; public class Main { private static void recursiveDeleteHelper(File parent, List<File> failed) { // delete children, depth first if (parent.isDirectory()) { for (File child : parent.listFiles()) { recursiveDeleteHelper(child, failed); } } // store leaves which can't be deleted in the failed list if (!parent.delete()) failed.add(parent); } /** * Deletes a file. * * @param file the file to delete * @throws IOException if the file cannot be deleted */ public static void delete(File file) throws IOException { if (!file.delete()) throw new IOException("Failed to delete " + file.getAbsolutePath()); } }