Here you can find the source of deleteFolder(String path)
Parameter | Description |
---|---|
path | - directory path |
public static boolean deleteFolder(String path)
//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; } }