Java File Path Delete deleteDir(File dir)

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

Description

Deletes the specified dir recursively, throwing a RuntimeException if the delete fails.

License

Apache License

Declaration

public static void deleteDir(File dir) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {
    /**/*ww w .j  a v  a2 s  .co m*/
     * Deletes the specified dir recursively, throwing a
     * {@link RuntimeException} if the delete fails.
     */
    public static void deleteDir(File dir) {
        deleteDirContent(dir);
        if (!dir.delete()) {
            throw new RuntimeException("Directory " + dir.getAbsolutePath()
                    + " can't be deleted.");
        }
    }

    /**
     * Fully delete the content of he specified directory.
     */
    public static void deleteDirContent(File dir) {
        final File[] files = dir.listFiles();
        if (files != null) {
            for (final File file : files) {
                if (file.isDirectory()) {
                    deleteDirContent(file);
                }
                file.delete();
            }
        }
    }

    /**
     * Deletes the specified file, throwing a {@link RuntimeException} if the
     * delete fails.
     */
    public static void delete(File file) {
        if (!file.delete()) {
            throw new RuntimeException("File " + file.getAbsolutePath()
                    + " can't be deleted.");
        }
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File dir)
  3. deleteDir(File dir)
  4. deleteDir(File dir)
  5. deleteDir(File dir)
  6. deleteDir(File dir)
  7. deleteDir(File dir)
  8. deleteDir(File dir)
  9. deleteDir(File dir)