Java Delete File Recursively deleteRecursive(File file)

Here you can find the source of deleteRecursive(File file)

Description

delete Recursive

License

Open Source License

Declaration

public static void deleteRecursive(File file) throws IOException 

Method Source Code

//package com.java2s;
/**//from w w w . ja  v a2  s . c om
 * Copyright (c) 2013 Puppet Labs, Inc. and other contributors, as listed below.
 * 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:
 *   Puppet Labs
 */

import java.io.File;

import java.io.IOException;

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static void deleteRecursive(File file) throws IOException {
        Set<File> visited = new HashSet<File>();
        deleteRecursive(file, visited);
    }

    private static void deleteRecursive(File file, Set<File> visited) throws IOException {
        if (visited.contains(file))
            throw new IOException("Circular file structure detected");

        visited.add(file);
        if (file.isDirectory()) {
            for (File subFile : file.listFiles())
                deleteRecursive(subFile, visited);
        }

        if (!file.delete()) {
            if (file.exists())
                throw new IOException("Unable to delete " + file.getAbsolutePath());
        }
    }

    public static void deleteRecursive(String path) throws IOException {
        deleteRecursive(new File(path));
    }
}

Related

  1. deleteRecursive(File file)
  2. deleteRecursive(File file)
  3. deleteRecursive(File file)
  4. deleteRecursive(File file)
  5. deleteRecursive(File file)
  6. deleteRecursive(File file)
  7. deleteRecursive(File root)
  8. deleteRecursive(File src)
  9. deleteRecursive(File src, List excludes)