Java Utililty Methods Delete File or Directory

List of utility methods to do Delete File or Directory

Description

The list of methods to do Delete File or Directory are organized into topic(s).

Method

voiddeleteFileOrDirectoryRecursive(String dir)
Recurse through children deleting all as we go
File theDir = new File(dir);
if (theDir != null) {
    deleteFileOrDirectoryRecursive(theDir);
booleandeleteFileOrDirOnExit(File filehandle)
Delete file or folder recursively on exit of the program
if (filehandle.isDirectory()) {
    String[] children = filehandle.list();
    for (int i = 0; i < children.length; i++) {
        boolean success = deleteFileOrDir(new File(filehandle, children[i]));
        if (!success) {
            return false;
filehandle.deleteOnExit();
return true;
voiddeleteFileOrFolder(File fileOrFolder)
Delete the file or folder given in fileOrFolder.
File[] files = fileOrFolder.listFiles();
if (files != null) {
    int idx = files.length;
    while (--idx >= 0)
        deleteFileOrFolder(files[idx]);
fileOrFolder.delete();