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)刪除失敗或該文件夾下不存在這類文件!");
}
}
}
個人認為:如果要實現(xiàn)更高級的這種模糊匹配,只需要用String的indexOf()方法,凡是含有這個字符串的文件,都一并刪除!
更多關于java算法相關內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
SpringBoot實現(xiàn)自定義指標監(jiān)控功能
本文主要介紹了SpringBoot實現(xiàn)自定義指標監(jiān)控功能的實現(xiàn),,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,感興趣的小伙伴跟著著小編來一起來學習吧2024-01-01
Spring中的singleton和prototype的實現(xiàn)
這篇文章主要介紹了Spring中的singleton和prototype的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Java實現(xiàn)數(shù)組去除重復數(shù)據(jù)的方法詳解
這篇文章主要介紹了Java實現(xiàn)數(shù)組去除重復數(shù)據(jù)的方法,結(jié)合實例形式詳細分析了java數(shù)組去除重復的幾種常用方法、實現(xiàn)原理與相關注意事項,需要的朋友可以參考下2017-09-09
Java報錯:java.util.concurrent.ExecutionException的解決辦法
在Java并發(fā)編程中,我們經(jīng)常使用java.util.concurrent包提供的工具來管理和協(xié)調(diào)多個線程的執(zhí)行,va并發(fā)編程中,然而,在使用這些工具時,可能會遇到各種各樣的異常,其中之一就是java.util.concurrent.ExecutionException,本文將詳細分析這種異常的背景、可能的原因2024-09-09

