使用file类delete方法和递归算法删除一个文件夹
注意:要求被删除的文件夹中存有子文件夹,子文件夹中还存储着各种文件和文件夹
package com.Work3;
import org.junit.Test;
import java.io.File;
/**
* @Author: 廾匸
* @Date: 2020/11/29 14:05
* @Description:
* @version: 1.01
*/
public class Three {
@Test
public void test01(){
boolean deletes = deletes(new File("G:\\线下"));
System.out.println("deletes = " + deletes);
}
public boolean deletes(File file){
if(file.isFile()){
file.delete();
return true;
}
File[] files = file.listFiles();
for (File file1 : files) {
if(file1 != null){
if(file1.isFile()){
file1.delete();
}else{
deletes(file1);
}
}
}
file.delete(); // 删除剩余空文件夹
return true;
}
}