Java File Path Delete deleteFolder(String path)

Here you can find the source of deleteFolder(String path)

Description

Delete all files and folders in the directory.

License

Apache License

Parameter

Parameter Description
path - directory path

Return

- true if successfully, otherwise false.

Declaration

public static boolean deleteFolder(String path) 

Method Source Code

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

import java.io.File;

public class Main {
    /**//  w w  w  .j a  va2s . co  m
     * Delete all files and folders in the directory. Directory may end with a
     * "\" or not.
     * 
     * @param path
     *            - directory path
     * @return - true if successfully, otherwise false.
     */
    public static boolean deleteFolder(String path) {
        boolean flag = true;
        File dirFile = new File(path);
        if (!dirFile.isDirectory()) {
            return flag;
        }
        File[] files = dirFile.listFiles();
        for (File file : files) {
            // Delete file.
            if (file.isFile()) {
                flag = deleteFile(file);
            } else if (file.isDirectory()) {// Delete folder
                flag = deleteFolder(file.getAbsolutePath());
            }
            if (!flag) {
                break;
            }
        }
        flag = dirFile.delete();
        return flag;
    }

    /**
     * Delete file according to a path.
     * 
     * @param path
     *            - file path
     * @return - true if delete successfully, otherwise false.
     */
    public static boolean deleteFile(String path) {
        File file = new File(path);
        return deleteFile(file);
    }

    /**
     * Delete file.
     * 
     * @param file
     *            - deleted file
     * @return - true if delete successfully, otherwise false.
     */
    public static boolean deleteFile(File file) {
        boolean flag = false;
        if (file.exists() && file.isFile()) {
            file.delete();
            flag = true;
        }
        return flag;
    }
}

Related

  1. deleteFolder(String path)
  2. deleteFolder(String path)
  3. deleteFolder(String path)
  4. deleteFolder(String path)
  5. deleteFolder(String path)
  6. deleteFolder(String path)
  7. deleteFolder(String path)
  8. deleteFolder(String pathToRootFolder)
  9. deleteFolder(String pFolderPath)