Java Delete File Recursively deleteRecursively(final File directory)

Here you can find the source of deleteRecursively(final File directory)

Description

delete Recursively

License

Apache License

Declaration

public static void deleteRecursively(final File directory) throws IOException 

Method Source Code

//package com.java2s;
/*//  ww w. j av a 2s . c  o m
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *       http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.File;

import java.io.IOException;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Main {
    private static int WINDOWS_RETRY_COUNT = 3;

    public static void deleteRecursively(final File directory) throws IOException {
        final Stack<File> stack = new Stack<>();
        final List<File> temp = new LinkedList<>();
        stack.push(directory.getAbsoluteFile());
        while (!stack.isEmpty()) {
            final File top = stack.pop();
            File[] files = top.listFiles();
            if (files != null) {
                for (final File child : files) {
                    if (child.isFile()) {
                        if (!deleteFile(child)) {
                            throw new IOException("Failed to delete " + child.getCanonicalPath());
                        }
                    } else {
                        temp.add(child);
                    }
                }
            }
            files = top.listFiles();
            if ((files == null) || (files.length == 0)) {
                if (!deleteFile(top)) {
                    throw new IOException("Failed to delete " + top.getCanonicalPath());
                }
            } else {
                stack.push(top);
                for (final File f : temp) {
                    stack.push(f);
                }
            }
            temp.clear();
        }
    }

    public static boolean deleteFile(final File file) {
        if (!file.exists()) {
            return true;
        }
        int count = 0;
        boolean deleted;
        do {
            deleted = file.delete();
            if (!deleted) {
                count++;
                waitSome();
            }
        } while (!deleted && (count <= WINDOWS_RETRY_COUNT));
        return deleted;
    }

    private static void waitSome() {
        try {
            Thread.sleep(500);
        } catch (final InterruptedException ee) {
            Thread.interrupted();
        } // ok
        System.gc();
    }
}

Related

  1. deleteRecursively(File name)
  2. deleteRecursively(File root)
  3. deleteRecursively(File root)
  4. deleteRecursively(File root, boolean deleteRoot)
  5. deleteRecursively(File[] roots)
  6. deleteRecursively(final File directory)
  7. deleteRecursively(final File iRootFile)
  8. deleteRecursivelyAtLeastOnExit(final File directory)
  9. deleteRecursivelyWithoutDeleteRootDirectory(File pFile)