java 递归删除指定文件夹下的所有内容

/**
 *删除文件夹下的文件
 *
 */
public static void delFiles(String filePath){
        File file = new File(filePath);
        if(!file.exists()){
            return;
        }
        String[] list = file.list();
        File temp = null;
        String path = null;
        for (String item:list) {
            path = filePath + File.separator + item;
            temp = new File(path);
            if(temp.isFile()){
                temp.delete();
                continue;
            }
            if(temp.isDirectory()) {
                delFiles(path);
                new File(path).delete();
                continue;
            }
        }
    }

    public static void main(String[] args){
        String filePath = "*********";
        delFiles(filePath);
    }

猜你喜欢

转载自blog.csdn.net/u013045878/article/details/81506668
今日推荐