Java Recursive Delete recursiveDelete(File file)

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

Description

Recursively delete the contents of a directory or just delete a file

License

Open Source License

Parameter

Parameter Description
file File or directory to delete

Declaration

public static void recursiveDelete(File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//  w  w  w  .  j  a  v a2s.  com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Recursively delete the contents of a directory or just delete a file
     * 
     * @param file
     *            File or directory to delete
     */
    public static void recursiveDelete(File file) {
        if (!file.exists())
            return;
        if (file.isDirectory()) {
            File[] contents = file.listFiles();
            for (int i = 0; i < contents.length; i++)
                recursiveDelete(contents[i]);
        }
        try {
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. recursiveDelete(File dirPath)
  2. recursiveDelete(File f)
  3. recursiveDelete(File f)
  4. recursiveDelete(File f)
  5. recursiveDelete(File f)
  6. recursiveDelete(File file)
  7. recursiveDelete(File file)
  8. recursiveDelete(File file)
  9. recursiveDelete(File file)