Java實現(xiàn)的模糊匹配某文件夾下的文件并刪除功能示例
本文實例講述了Java實現(xiàn)的模糊匹配某文件夾下的文件并刪除功能。分享給大家供大家參考,具體如下:
package com.wyebd.gis; import java.io.File; /** * @Title: DelFiles.java * @Package com.wyebd.gis * @Description: * @author lisr * @date Mar 7, 2012 5:36:03 PM * @version V1.0 */ public class DelFiles { /** * @Title: main * @Description: * @param args * @return void * @author lisr * @date Mar 7, 2012 5:36:04 PM * @throws */ //用以模糊刪除頭部為str的文件 public static boolean delFilesByPath(String path,String str){ //參數(shù)說明---------path:要刪除的文件的文件夾的路徑---------str:要匹配的字符串的頭 boolean b=false; File file = new File(path); File[] tempFile = file.listFiles(); for(int i = 0; i < tempFile.length; i++){ if(tempFile[i].getName().startsWith(str)||tempFile[i].getName().endsWith(str)){ System.out.println("將被刪除的文件名:"+tempFile[i].getName()); boolean del=deleteFile(path+tempFile[i].getName()); if(del){ System.out.println("文件"+tempFile[i].getName()+"刪除成功"); b=true; }else{ System.out.println("文件"+tempFile[i].getName()+"刪除失敗"); } } } return b; } private static boolean deleteFile(String path){ System.out.println(path); boolean del=false; File file=new File(path); if(file.isFile()){ file.delete(); del=true; } return del; } public static void main(String[] args) { // TODO Auto-generated method stub String path="D:/temp/"; String str="44_"; if(delFilesByPath(path,str)){ System.out.println(path+"中包含"+str+"的文件已經(jīng)全部刪除成功!"); }else{ System.out.println(path+"中包含"+str+"的文件已經(jīng)刪除失敗或該文件夾下不存在這類文件!"); } } }
package com.wyebd.gis; import java.io.File; /** * @Title: DelFiles.java * @Package com.wyebd.gis * @Description: * @author lisr * @date Mar 7, 2012 5:36:03 PM * @version V1.0 */ public class DelFiles { /** * @Title: main * @Description: * @param args * @return void * @author lisr * @date Mar 7, 2012 5:36:04 PM * @throws */ //用以模糊刪除頭部為str的文件 public static boolean delFilesByPath(String path,String str){ //參數(shù)說明---------path:要刪除的文件的文件夾的路徑---------str:要匹配的字符串的頭 boolean b=false; File file = new File(path); File[] tempFile = file.listFiles(); for(int i = 0; i < tempFile.length; i++){ if(tempFile[i].getName().startsWith(str)||tempFile[i].getName().endsWith(str)){ tempFile[i].delete(); b=true; } } return b; } public static void main(String[] args) { String path="D:/temp/"; String str="44_"; if(delFilesByPath(path,str)){ System.out.println(path+"中包含"+str+"的文件已經(jīng)全部刪除成功!"); }else{ System.out.println(path+"中包含"+str+"的文件已經(jīng)刪除失敗或該文件夾下不存在這類文件!"); } } }
個人認(rèn)為:如果要實現(xiàn)更高級的這種模糊匹配,只需要用String的indexOf()
方法,凡是含有這個字符串的文件,都一并刪除!
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Springboot解決跨域問題方案總結(jié)(包括Nginx,Gateway網(wǎng)關(guān)等)
跨域問題是瀏覽器為了保護用戶的信息安全,實施了同源策略(Same-Origin?Policy),即只允許頁面請求同源(相同協(xié)議、域名和端口)的資源,本文給大家總結(jié)了Springboot解決跨域問題方案包括Nginx,Gateway網(wǎng)關(guān)等),需要的朋友可以參考下2024-03-03深入理解JAVA中的聚集和組合的區(qū)別與聯(lián)系
下面小編就為大家?guī)硪黄钊肜斫釰AVA中的聚集和組合的區(qū)別與聯(lián)系。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨小編過來看看吧2016-05-05springboot+jwt+springSecurity微信小程序授權(quán)登錄問題
這篇文章主要介紹了springboot+jwt+springSecurity微信小程序授權(quán)登錄問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Mybatis中xml的動態(tài)sql實現(xiàn)示例
本文主要介紹了Mybatis中xml的動態(tài)sql實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Springboot Cache @CacheEvict 無法模糊刪除的解決方案
這篇文章主要介紹了Springboot Cache @CacheEvict 無法模糊刪除的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12