java刪除本地文件夾下的所有文件

定義函數

/**
     * 刪除指定文件夾下的所有文件
     * @param path
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("111");
            return flag;
        }
        if (!file.isDirectory()) {
            System.out.println("222");
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                System.out.println("225"+temp);
                temp = new File(path + tempList[i]);
            } else {
                System.out.println("2244"+temp);
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                System.out.println("111"+temp);
                System.out.println("666"+tempList);
                temp.delete();
            }
        }
        return flag;
    }

調用

delAllFile("D:/log/video");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章