Java Recursive Delete recursiveDelete(File base)

Here you can find the source of recursiveDelete(File base)

Description

Recursively deletes all files in the tree rooted at the given path.

License

Open Source License

Parameter

Parameter Description
base the root to delete

Exception

Parameter Description
IOException if some file cannot be deleted

Declaration

public static void recursiveDelete(File base) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w ww  .j ava  2s . 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.ArrayList;
import java.util.List;

public class Main {
    /**
     * Recursively deletes all files in the tree rooted at the given path.
     *
     * @param base the root to delete
     * @throws IOException if some file cannot be deleted
     */
    public static void recursiveDelete(File base) throws IOException {
        // we delete as many files as we can
        final List<File> failed = new ArrayList<File>();
        recursiveDeleteHelper(base, failed);

        // if any deletions failed, we list them
        if (!failed.isEmpty()) {
            final StringBuilder sb = new StringBuilder("Failed to delete");
            for (File f : failed)
                sb.append(' ').append(f.getAbsolutePath());
            throw new IOException(sb.toString());
        }
    }

    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());
    }
}

Related

  1. recursiveDel(final File f)
  2. recursiveDelete(File _f)
  3. recursiveDelete(File dir)
  4. recursiveDelete(File dir)
  5. recursiveDelete(File dir)
  6. recursiveDelete(File dir, boolean deleteBase)