国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Java編程迭代地刪除文件夾及其下的所有文件實(shí)例

2020-02-22 23:12:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文研究的是Java編程迭代地刪除文件實(shí)例,具體實(shí)現(xiàn)代碼如下。

實(shí)例代碼:

public static void main(String[] args) {     String filePath = "c:" + File.separator +"b";     File file = new File(filePath);     if (file.exists()) {       if (file.isFile()) {         deleteFile(filePath);       } else {         deleteDirectory(filePath);       }     } else {       System.err.println("指定的目錄或者文件不存在!");     }   }   //刪除單個(gè)文件或空的文件夾   public static boolean deleteFile(String filePath) {     File file = new File(filePath);     //如果文件路徑對(duì)應(yīng)的文件存在,并且是一個(gè)文件,則直接刪除     if (file.exists() && file.isFile()) {       if (file.delete()) {         System.err.println("文件" + filePath + "刪除成功!");         return true;       } else {         System.err.println("文件" + filePath + "刪除失敗!");         return false;       }     } else {       System.err.println("文件" + filePath + "不存在!");       return false;     }   }   //刪除文件夾及里面的文件   public static boolean deleteDirectory (String dir) {     if (!dir.endsWith(File.separator)) {       dir = dir + File.separator;     }     File dirFile = new File(dir);     //如果dir對(duì)應(yīng)的問(wèn)件不存在,或者不是一個(gè)目錄,則退出     if (!dirFile.exists() || !dirFile.isDirectory()) {       System.err.println("文件夾" + dir + "不存在!");       return false;     }     boolean flag = true;     //刪除問(wèn)價(jià)夾中的所有文件包括子目錄     File[] files = dirFile.listFiles();     for (int i = 0; i < files.length; i++) {       //刪除子文件       if (files[i].isFile()) {         flag = deleteFile(files[i].getAbsolutePath());         if (!flag) {           break;         }       } else {         deleteDirectory(files[i].getAbsolutePath());       }     }     //刪除當(dāng)前目錄     if (dirFile.delete()) {       System.err.println("目錄" + dir + "刪除成功!");       return true;     } else {       return false;     }   } 

總結(jié)

以上就是本文關(guān)于Java編程迭代地刪除文件夾及其下的所有文件實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 扶沟县| 澜沧| 张家口市| 揭东县| 吉木乃县| 黄骅市| 桃江县| 于田县| 永靖县| 永新县| 锡林浩特市| 缙云县| 孟连| 无为县| 龙胜| 商水县| 新竹市| 滨州市| 绿春县| 东乌珠穆沁旗| 浙江省| 锡林郭勒盟| 玉溪市| 肃宁县| 图木舒克市| 吐鲁番市| 苏州市| 神池县| 山阳县| 玉林市| 西昌市| 肇东市| 报价| 德州市| 怀来县| 兰西县| 吴桥县| 新竹县| 邛崃市| 通城县| 庄河市|