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);
    }

 

你可能感兴趣的:(program)