DeleteFolderDemo.java
import java.io.File;
import java.io.IOException;
public class DeleteFolderDemo {
public static void main(String[] args) {
if (deletePath("000017PINC0000000962", "2017-10-09")) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
public static boolean deletePath(String batchNo, String transactionDate) {
StringBuffer path = new StringBuffer();
path.append("D:\\test\\hahha");
path.append(File.separator);
path.append(transactionDate);
path.append(File.separator);
path.append(batchNo);
path.append(File.separator);
File file = new File(path.toString());
System.out.println("路径:"+ path.toString());
//方法一
/*try {
org.apache.commons.io.FileUtils.deleteDirectory(file);
return true;
} catch (IOException e) {
System.out.println("删除异常!");
return false;
}*/
//方法二
return deleteDir(file);
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
*
* @param dir
* 将要删除的文件目录
* @return
*/
private static boolean deleteDir(File dir) {
if (!dir.exists()) return false;
if (dir.isDirectory()) {
String[] childrens = dir.list();
// 递归删除目录中的子目录下
for (String child : childrens) {
boolean success = deleteDir(new File(dir, child));
if (!success) return false;
}
}
// 目录此时为空,可以删除
return dir.delete();
}
}