欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java递归删除指定文件夹下所有文件

程序员文章站 2022-03-03 10:36:23
...

Java递归删除指定文件夹下所有文件


工具类封装

public class FileUtils{
	
	public static boolean delAllFile(String path) {
	    return delAllFile(new File(path));
	}
	
	public static boolean delAllFile(File path) {
	    boolean flag = false;
	    if (!path.exists()) {
	        return flag;
	    }
	    if (!path.isDirectory()) {
	        return flag;
	    }
	    String[] fileList = path.list();
	    File file;
	    for (int i = 0; i < fileList.length; i++) {
	        file = new File(path + File.separator + fileList[i]);
	        if (file.isFile()) {
	            file.delete();
	        }
	        if (file.isDirectory()) {
	            delAllFile(file);
	            file.delete();
	            flag = true;
	        }
	    }
	    return flag;
	}
}
相关标签: 工具类