/**
* @ClassName FileOperationTool
* @Description TODO(这里用一句话描述这个类的作用)
* @author feizhou
* @Date 2018年4月20日 下午1:48:14
* @version 1.0.0
*/
public class FileOperationTool {
/**
*
* @Description (删除目录下的所有文件,包括目录)
* @author feizhou
* @Date 2018年4月20日下午2:14:34
* @version 1.0.0
* @param path
* @return
*/
public static void delDirectoryIncludeDirectory(String folderPath) {
folderPath=folderPath.trim();
//删除完里面所有内容
delDirectoryNotIncludeDirectory(folderPath);
File file = new File(folderPath);
//删除空文件夹
file.delete();
}
/**
*
* @Description (删除目录下的所有文件,不包括目录)
* @author feizhou
* @Date 2018年4月20日下午2:14:34
* @version 1.0.0
* @param path
* @return
*/
public static boolean delDirectoryNotIncludeDirectory(String path) {
boolean flag = false;
File file = new File(path);
//文件或者目录不存在
if (!file.exists()) {
return flag;
}
//不是目录
if (!file.isDirectory()) {
return flag;
}
//获取目录下的所有文件名称
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
//如果路径已/结尾, 路径+名称
temp = new File(path + tempList[i]);
} else {
//如果路径不是已/结尾, 路径+/+名称
temp = new File(path + File.separator + tempList[i]);
}
//如果是文件
if (temp.isFile()) {
temp.delete();
}
//如果是目录,递归删除
if (temp.isDirectory()) {
delDirectoryNotIncludeDirectory(path + "/" + tempList[i]);//先删除文件夹里面的文件
delDirectoryIncludeDirectory(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
public static void main(String[] args) {
String path="E:\\test\\2222";
delDirectoryIncludeDirectory(path);
}
目录已经删除
可以对比看2222副本