Java Delete File Recursively deleteRecursive(File dir)

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

Description

delete Recursive

License

Open Source License

Declaration

public static void deleteRecursive(File dir) 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 {
    public static void deleteRecursive(File dir) throws IOException {
        if (!dir.exists())
            return;

        deleteContentsRecursive(dir);//from  w  w w .  ja  v a  2  s  . c om
        if (!dir.delete())
            throw new IOException("Unable to delete " + dir);
    }

    /**
     * Deletes the contents of the given directory (but not the directory itself)
     * recursively.
     *
     * @throws IOException
     *      if the operation fails.
     */
    public static void deleteContentsRecursive(File file) throws IOException {
        File[] files = file.listFiles();
        if (files == null)
            return; // non existent
        for (File child : files) {
            if (child.isDirectory())
                deleteContentsRecursive(child);
            if (!child.delete())
                throw new IOException("Unable to delete " + child.getPath());
        }
    }
}

Related

  1. deleteR(File dir)
  2. deleteRec(File f)
  3. deleteRecursive(File dir)
  4. deleteRecursive(File dir)
  5. deleteRecursive(File dir)
  6. deleteRecursive(File dir)
  7. deleteRecursive(File f)
  8. deleteRecursive(File f)
  9. deleteRecursive(File f)