Here you can find the source of clearFolder(String path)
Parameter | Description |
---|---|
path | - directory path |
public static boolean clearFolder(String path)
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { /**//from www. ja v a 2 s.c om * Delete all files in the directory except folders. Directory may end with * a "\" or not. * * @param path * - directory path * @return - true if successfully, otherwise false. */ public static boolean clearFolder(String path) { boolean flag = true; File dirFile = new File(path); if (!dirFile.isDirectory()) { return flag; } File[] files = dirFile.listFiles(); for (File file : files) { // Delete files. if (file.isFile()) { flag = deleteFile(file); if (!flag) { break; } } } 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; } }