Java Recursive Delete recursiveDelete(File pathToFolderOrFile)

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

Description

Deletes file or folder.

License

Apache License

Parameter

Parameter Description
pathToFolderOrFile a parameter

Declaration

public static boolean recursiveDelete(File pathToFolderOrFile) 

Method Source Code


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

import java.io.*;

public class Main {
    /**//  ww w  .j  a  v  a2 s .  co m
     * Deletes file or folder. Folder could contain files or even other folders
     *
     * @param pathToFolderOrFile
     * @return
     */
    public static boolean recursiveDelete(File pathToFolderOrFile) {
        if (pathToFolderOrFile.isFile() && pathToFolderOrFile.exists()) {
            pathToFolderOrFile.delete();
        } else if (pathToFolderOrFile.exists()) {
            File[] files = pathToFolderOrFile.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    recursiveDelete(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (pathToFolderOrFile.delete());
    }
}

Related

  1. recursiveDelete(File file, boolean deleteParentFile)
  2. recursiveDelete(File fileOrDir)
  3. recursiveDelete(File fileOrDir)
  4. recursiveDelete(File folder)
  5. recursiveDelete(File parent)
  6. recursiveDelete(File root)
  7. recursiveDelete(File root)
  8. recursiveDelete(File rootDir, boolean deleteRoot)
  9. recursiveDelete(File target)