1、创建多层文件夹目录
2、采用递归的方法将一个文件夹以及它的子目录全部删除
import java.io.File;
public class FileOperatorUtil {
//创建多层文件夹目录
public static boolean createRootDir(String dirPath){
File file = new File(dirPath);
if(file.exists()){
//如果有则不用创建
return true;
}
return file.mkdirs();
}
//递归删除某文件夹以及它的子目录
public static boolean deleteDirectory(String dirPath) {
if (dirPath == null || dirPath.trim().length() == 0) {
return false;
}
File file =new File(dirPath);
//是文件直接删除
if(file.isFile()){
if(!file.delete()){
return false;
}
}else{
//是文件夹,遍历本文件夹成file数组递归删除
File[] subFileArray=file.listFiles();
for (File subFile : subFileArray) {
deleteDirectory(subFile.toString());
}
if(file.exists() ){
if(!file.delete()){
return false;
}
}
}
return true;
}
//工具类测试
public static void main(String[] args) {
// String dirPath="a/b/c";
// boolean flag = createRootDir(dirPath);
// System.out.println(flag);
String dirPath= "a/b/c";
boolean flag =deleteDirectory(dirPath);
System.out.println(flag);
}
}