本文實例為大家分享了java刪除指定目錄下指定格式文件的具體代碼,供大家參考,具體內容如下
正在看瘋狂java講義這本書,發現源碼中有我不需要的class文件,想批量把它刪除
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import java.io.file; public class main { static int count = 0 ; public static void main(string[] args) { //路徑 string path= "/media/lcy/data/workspaces/java/crazyjava" ; string geshi= ".class" ; refreshfilelist(path,geshi); system.out.println( "共刪除了:" + count + "個文件!" ); } public static void refreshfilelist(string strpath,string geshi) { file dir = new file(strpath); file[] files = dir.listfiles(); if (files == null ) { system.out.println( "該目錄下沒有任何一個文件!" ); return ; } for ( int i = 0 ; i < files.length; i++) { if (files[i].isdirectory()) { refreshfilelist(files[i].getabsolutepath(),geshi); } else { string strfilename = files[i].getabsolutepath().tolowercase(); if (strfilename.endswith(geshi)){ system.out.println( "正在刪除:" + strfilename); files[i].delete(); count++; } } } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/LCYong_/article/details/78115768