Java Delete File Recursively deleteRecursive(File toDelete)

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

Description

This method acts much like bash's "rm -rf $file" command.

License

Open Source License

Parameter

Parameter Description
folderToDelete Folder to delete with all of its contents

Return

Whether the delete was successful

Declaration

public static void deleteRecursive(File toDelete) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

public class Main {
    /**//from   www.  ja v a  2 s .c om
     * This method acts much like bash's "rm -rf $file" command. It will
     * delete the directory and all of its contents! If the File passed is
     * not a folder, it will be deleted, too.
     * 
     * **This WILL follow symlinks! Be careful!
     * 
     * @param folderToDelete Folder to delete with all of its contents
     * @return Whether the delete was successful
     */
    public static void deleteRecursive(File toDelete) throws IOException {
        for (File sub : toDelete.listFiles()) {
            if (sub.isDirectory()) {
                deleteRecursive(sub);
            } else {
                if (!sub.delete()) {
                    throw new IOException();
                }
            }
        }
    }
}

Related

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