Java File Delete doDeleteFileOrDir(File fileOrDir)

Here you can find the source of doDeleteFileOrDir(File fileOrDir)

Description

Deletes specified file or directory.

License

Open Source License

Parameter

Parameter Description
fileOrDir file or directory to be deleted

Return

true if specified file or directory has been deleted; false otherwise

Declaration

public static boolean doDeleteFileOrDir(File fileOrDir) 

Method Source Code


//package com.java2s;

import java.io.File;

public class Main {
    /**/*w w w  . j a  v  a2  s.com*/
     * Deletes specified file or directory. A directory is recursively
     * deleted.
     * 
     * @param fileOrDir file or directory to be deleted
     * @return <code>true</code> if specified file or directory has been
     * deleted; <code>false</code> otherwise
     */
    public static boolean doDeleteFileOrDir(File fileOrDir) {
        if (fileOrDir.isDirectory() && !doEmptyDir(fileOrDir)) {
            return false;
        }

        return fileOrDir.delete();
    }

    /**
     * Removes all the files contained in specified directory.
     * 
     * @param dir directory to be made empty
     * @return <code>true</code> if the directory has been made empty;
     * <code>false</code> otherwise
     */
    public static boolean doEmptyDir(File dir) {
        File[] children = dir.listFiles();
        if (children == null) {
            return false;
        }

        for (int i = 0; i < children.length; ++i) {
            File child = children[i];

            if (child.isDirectory() && !doEmptyDir(child)) {
                return false;
            }

            if (!child.delete()) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. doDelete(File file)
  2. doDelete(File file)
  3. doDelete(File path)
  4. doDeleteEmptyDir(String dir)
  5. doDeleteEmptyParentQuietly(File parent)
  6. doDeleteQuietly(File file)
  7. fileDelete(String fileName)
  8. fileDelete(String path)
  9. fileDelete(String path)