java contains 通配符_java刪除文件支持通配符

packagecom.covics.zfh;importjava.io.File;importjava.io.FilenameFilter;/*** 實現帶*號的刪除文件

*@authordoudou

**/

public classTestDeleteFile {public static voidmain(String args[]) {boolean delFile =

//windows//deleteFileWithSign("E:\\fileDeleteTest\\test\\t*ve.txt");//linux

deleteFileWithSign("/home/work/ssst*ve.txt");

System.out.println("main---->"+delFile);

}/*** 刪除帶*的文件

*@paramfileName

*@return

*/

public static booleandeleteFileWithSign(String fileName) {//文件夾名稱

String dirName = "";//要刪除的文件類型

String fileType = "";//不帶后綴的文件名

String nameWithoutType = "";//*號個數

int num = 0;if (null == fileName || "".equals(fileName)) {return false;

}

String osName= System.getProperty("os.name")if (osName.toLowerCase().contains("windows")){int index = fileName.lastIndexOf("\\") + 1;

}else{int index = fileName.lastIndexOf("/") + 1;

}

nameWithoutType= fileName.substring(index, fileName.lastIndexOf("."));

fileType=fileName

.substring(fileName.lastIndexOf("."), fileName.length());

dirName= fileName.substring(0, index);

File dir= newFile(dirName);

System.out.println("dir:"+dir);

System.out.println("exits"+dir.exists());

System.out.println("dir.isDirectory()"+dir.isDirectory());if (!dir.exists() || !dir.isDirectory()) {return false;

}//*號的位置

int start = -1, end = -1;

start= nameWithoutType.indexOf("*");

end= nameWithoutType.lastIndexOf("*");int length =nameWithoutType.length();for (int i=0;i

num++;

}

}

System.out.println("num---->"+num);//刪除的類型

int delType = 0;switch(num) {case 0:returndeleteFile(fileName);case 1 : //只有一個*號的情況

if (0==start) { //在開始的位置

delType = 1; //只調用endWith方法

} else if (length-1 == start) { //在結束的位置

delType = 2; //只調用startWith方法

} else { //在中間

delType = 3; //只調用contain方法

}break;default : //有兩個或多個*號的情況

if (0 == start && length-1 == end) { //在開始的位置和結束的位置都有*號

delType = 4; //只調用contain方法

} else if (0 == start && length-1 != end) { //開始的位置有*號且結束的位置沒有*號

delType = 5; //調用contain和endWith方法

} else if (0 != start && length-1 == end) { //開始的位置沒有*號且結束的位置有*號

delType = 6; //調用startWith和contain方法

} else { //*號在中間 的情況(開始和結束都沒有*號)

delType = 7; //只調用contain方法

}break;

}boolean flag = false;

String[] names=dir

.list(newDelFileFilter(nameWithoutType, fileType, delType));

System.out.println("names.length---->"+names.length);for (int i = 0; i < names.length; i++) {

System.out.println("f--names-->"+dirName+names[i]);

File f= new File(dirName +names[i]);if (!f.exists() || !f.isFile()) {return false;

}

flag=deleteFile(f.getAbsolutePath());if (!flag) {break;

}

}returnflag;

}/*** 刪除單個文件

*

*@paramfileName

*@return

*/

public static booleandeleteFile(String fileName) {

File file= newFile(fileName);if (file.isFile() &&file.exists()) {

file.delete();return true;

}return false;

}/*** 刪除文件夾

*

*@paramdir

*@return

*/

public static booleandeleteDir(String dir) {//判斷是否有帶文件夾符號。

if (!dir.endsWith(File.separator)) {

dir= dir +File.separator;

}

File file= newFile(dir);if (!file.isDirectory() || !file.exists()) {return false;

}boolean flag = true;

File[] files=file.listFiles();for (int i = 0; i < files.length; i++) {if (files[i].isFile()) { //刪除文件

flag =deleteFile(files[i].getAbsolutePath());if (!flag) {break;

}

}else if (files[i].isDirectory()) { //刪除文件夾

flag =deleteDir(files[i].getAbsolutePath());if (!flag) {break;

}

}else{return false;

}

}if (file.delete()) { //刪除自身

return true;

}return false;

}

}/*** FileFilter類

* 刪除帶*文件

*@authordoudou

**/

class DelFileFilter implementsFilenameFilter {//傳進來的文件URL

String fileName;

String fileType;//文件類型//刪除的類型。 1:*號在開頭; 2:*號在末尾

intdelType;public DelFileFilter(String name, String fileType, intdelType) {this.fileType =fileType;this.delType =delType;this.fileName =name;

}

@Overridepublic booleanaccept(File dir, String name) {//將文件名分割顧字符串

String[] ss = fileName.split("[*]");//首先判斷是否是要刪除的文件類型

if (!isThisTypeFile(name)) {return false;

}boolean flag = true;switch(delType) {case 1:for(String s : ss) {if (!"".equals(s)){

flag=endWithS(name, s);

}

}returnflag;case 2:for(String s : ss) {if (!"".equals(s)){

flag=startWithS(name, s);

}

}returnflag;case 3:case 4:case 7:for(String s : ss) {if (!"".equals(s)) {

flag=contains(name, s);if (!flag) {break;

}

}

}returnflag;case 5:boolean flag1 = false, flag2 = false;for (int i=0;i

flag1=contains(name, ss[i]);if (!flag1) {break;

}

}if (i == ss.length-1) {

flag2=endWithS(name, ss[i]);

}

}

}

flag= flag1 &&flag2;returnflag;case 6:boolean flag3 = false, flag4 = false;

flag3= startWithS(name, ss[0]);for (int i=1;i

flag4=contains(name, ss[i]);if (!flag4)break;

}

}

flag= flag3 &&flag4;returnflag;default:

flag= false;returnflag;

}

}/*** 判斷是否是該類型的文件

*@paramfile

*@return

*/

public booleanisThisTypeFile(String file) {returnfile.toLowerCase().endsWith(fileType);

}/*** 包含某字符

*@paramfile

*@paramstr

*@return

*/

public booleancontains(String file, String str) {return(file.toLowerCase().contains(str));

}/*** 以某字符開始

*@paramfile

*@paramstr

*@return

*/

public booleanstartWithS(String file, String str) {returnfile.toLowerCase().startsWith(str);

}/*** 以file字符結束

*@paramfile

*@paramstr

*@return

*/

public booleanendWithS(String file, String str) {return file.toLowerCase().endsWith(str+fileType);

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275791.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275791.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275791.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

「線性基」學習小結

向量空間 向量空間亦稱線性空間。 形式化的&#xff0c;我們定義一個向量空間\((P,V,,\cdot)\) 其中 \(P\)是一個域&#xff0c;\(V\)是一個非空的集合&#xff0c;其中的集合稱作向量&#xff0c;同時定義兩種運算分別為向量加法和標量乘法 一個\(P\)上的向量空間\((P,V,,\cdo…

ux體驗網站 英國_定義網站圖像時的UX注意事項

ux體驗網站 英國As the saying goes —俗話說 - “A picture is worth a thousand words.”“一張圖片勝過千言萬語。” When creating content on the web, it’s often recommended to be using high-quality imageries and making sure that the images serve its purpose …

iconfont 支持全新的彩色字體圖標

大家好&#xff0c;我是若川。iconfont我相信大家都用過&#xff0c;而現在支持全新的彩色字體圖標了。這是第二次轉載&#xff0c;上一次的好文是2020 前端技術發展回顧。點擊下方卡片關注我、加個星標學習源碼整體架構系列、年度總結、JS基礎系列一直以來&#xff0c;Web 中想…

回文算法java實現_java算法題:最長回文串

LeetCode: 給定一個包含大寫字母和小寫字母的字符串&#xff0c;找到通過這些字母構造成的最長的回文串。在構造過程中&#xff0c;請注意區分大小寫。比如"Aa"不能當做一個回文字符串。注 意:假設字符串的長度不會超過 1010。思路&#xff1a;利用hashset&#xff0…

Spring校驗@RequestParams和@PathVariables參數

我們在寫Rest API接口時候會用到很多的RequestParam和PathVariable進行參數的傳遞&#xff0c;但是在校驗的時候&#xff0c;不像使用RequestBody那樣的直接寫在實體類中&#xff0c;我們這篇文章講解一下如何去校驗這些參數。 依賴配置 要使用Java Validation API&#xff0c;…

出色的社區網站_《最后的我們》中出色的制作系統

出色的社區網站游戲設計分析 (GAME DESIGN ANALYSIS) The Last of Us became an instant classic the day it was released, back in 2013. At the sunset of the sixth console generation, it felt like Naughty Dog managed to raise the bar in all critical areas of game…

入坑 Electron 開發跨平臺桌面應用

?作為一個跨平臺的桌面應用開發框架&#xff0c;Electron 的迷人之處在于&#xff0c;它是建立在 Chromium 和 Node.js 之上的 —— 二位分工明確&#xff0c;一個負責界面&#xff0c;一個負責背后的邏輯&#xff0c;典型的「你負責貌美如花&#xff0c;我負責賺錢養家」。上…

Google 拼音會導致卡 Ctrl 鍵?

如果你使用 Windows 7 系統&#xff0c;并同時安裝了 Google 輸入法&#xff0c;那么 Firefox 啟動時、WoW 時一定也常遇到卡住 Ctrl 鍵的問題。 今天仔細搜索了下&#xff0c;傳說將輸入法中“Ctrl鍵快速定位”關閉即可&#xff0c;有待驗證&#xff0c;先記錄著…轉載于:http…

java 接口編程_JAVA面向接口編程

一、什么是面向接口編程要正確地使用Java語言進行面向對象的編程&#xff0c;從而提高程序的復用性&#xff0c;增加程序的可維護性、可擴展性&#xff0c;就必須是面向接口的編程。面向接口的編程就意味著&#xff1a;開發系統時&#xff0c;主體構架使用接口&#xff0c;接口…

不僅僅是手機,MWC現全球首例 5G NR 商用部署

近日&#xff0c;MWC大會在在巴塞羅那舉行&#xff0c;5G折疊手機和5G部署進度成為這屆大會的重點。除了華為與三星發布的折疊手機外&#xff0c;本屆大會另一個值得關注的要點是三星和賽靈思宣布推進5G NR 商用部署在韓國落地&#xff0c;這應該是全球首例 5G 新無線電 (NR) 商…

小程序 顯示細線_精心設計:高密度顯示器上的細線

小程序 顯示細線Despite the many benefits of Retina displays, there is one clear drawback that must be considered when designing for high-density screens:盡管Retina顯示器具有許多優點&#xff0c;但在設計高密度屏幕時仍必須考慮一個明顯的缺點&#xff1a; 必須避…

React 入門手冊

大家好&#xff0c;我是若川。推薦這篇可收藏的React入門手冊。也推薦之前一篇類似的文章《如何使用 React 和 React Hooks 創建一個天氣應用》。點擊下方卡片關注我、加個星標React 是目前為止最受歡迎的 JavaScript 框架之一&#xff0c;而且我相信它也是目前最好用的開發工具…

函數04 - 零基礎入門學習C語言35

第七章&#xff1a;函數04 讓編程改變世界 Change the world by program 上節課的練習簡單講解,給力&#xff01;&#xff01; 1.自己實現pow()函數并嘗試驗證…… 2.猜想下sqrt()函數的原理并嘗試編程……&#xff08;暫時只要求整型數據&#xff09; 3.編寫一個用來統…

java數據結構與算法_清華大學出版社-圖書詳情-《數據結構與算法分析(Java版)》...

前 言數據結構是計算機程序設計重要的理論技術基礎&#xff0c;它不僅是計算機學科的核心課程&#xff0c;而且已經成為計算機相關專業必要的選修課。其要求是學會分析、研究計算機加工的數據結構的特性&#xff0c;初步掌握算法的時間和空間分析技術&#xff0c;并能夠編寫出結…

根號 巴比倫_建立巴比倫衛生設計系統

根號 巴比倫重點 (Top highlight)In this post I’ll explain the first phase of creating our Babylon DNA, the design system for Babylon Health, and how we moved the Babylon design team from Sketch to Figma.在這篇文章中&#xff0c;我將解釋創建巴比倫DNA的第一階…

《Migrating to Cloud-Native Application Architectures》學習筆記之Chapter 2. Changes Needed

2019獨角獸企業重金招聘Python工程師標準>>> Cultural Change 文化變革 A great deal of the changes necessary for enterprise IT shops to adopt cloud-native architectures will not be technical at all. They will be cultural and organizational changes t…

前端,你要知道的SEO知識

大家好&#xff0c;我是若川。三天假期總是那么短暫&#xff0c;明天就要上班了。今天推薦一篇相對簡單的文章。點擊下方卡片關注我、加個星標之前有同學在前端技術分享時提到了SEO&#xff0c;另一同學問我SEO是什么&#xff0c;我當時非常詫異&#xff0c;作為前端應該對SEO很…

編制網站首頁的基本原則

編制網站首頁的基本原則如下&#xff1a; 1、編制網站首頁的超文本文檔的組織結構應清晰&#xff0c;條理分明&#xff0c;重點突出&#xff0c;可讀性強&#xff0c;盡可能吸引用戶的注意力。 2、說明文字應簡明扼要&#xff0c;切中要害&#xff0c;每項內容介紹盡可能簡單明…

MySQL數據庫語句總結

增insert into -- 刪 delete from -- 改 update table名字 set -- 查 select * from -- 一&#xff0e;SQL定義 SQL&#xff08;Structure Query Language&#xff09;結構化查詢語言&#xff1a; &#xff08;一&#xff09;DDL&#xff08;Data Definition Language&#…

高安全性同態加密算法_壞的同態性教程

高安全性同態加密算法I was going to write at length about the issues I see in neumorphism and why this trend should be avoided. I know any attempt to guide my most impressionable colleagues away from it, will end up being failing because this fad is going t…