Java Delete Folder deleteFolder(File dir, boolean deleteFolderItself)

Here you can find the source of deleteFolder(File dir, boolean deleteFolderItself)

Description

Deletes given folder with all of its content

License

Open Source License

Parameter

Parameter Description
dir a parameter
deleteFolderItself a parameter

Declaration

public static void deleteFolder(File dir, boolean deleteFolderItself) 

Method Source Code


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

import java.io.File;

public class Main {
    /**//w  ww  . j  a  v  a 2  s.c o m
     * Deletes given folder with all of its content
     * 
     * @param dir
     * @param deleteFolderItself
     */
    public static void deleteFolder(File dir, boolean deleteFolderItself) {
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory()) {
                deleteFolder(f, true);
            } else if (f.isFile()) {
                if (!f.delete()) {
                    System.err.println("Cannot delete file " + f.getAbsolutePath());
                }
            }
        }
        if (deleteFolderItself) {
            if (!dir.delete()) {
                System.err.println("Cannot delete folder " + dir.getAbsolutePath());
            }
        }
    }
}

Related

  1. deleteFolder(@Nonnull File file)
  2. deleteFolder(File file)
  3. deleteFolder(File file)
  4. deleteFolder(File file)
  5. deleteFolder(File file, boolean deleteParent)