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
|
package file; import java.io.file; /** * 輸出某個文件夾下所有某個格式的文件 * @author hasee * */ public class demo2 { public static void main(string[] args) { gettxtname( "d:/a" , ".jpg" ); } public static void gettxtname(string path,string suffix) { //判斷文件對象是文件還是文件夾 //構建文件對象 file f = new file(path); //根據文件或者文件夾處理 if (f.isfile()) { if (f.getname().endswith(suffix)) { system.out.println(f.getabsolutepath()); } } else { //遍歷文件夾 file[] files = f.listfiles(); if (files!= null && files.length> 0 ) { //繼續遞歸得到的文件或文件夾 for (file file : files) { gettxtname(file.getabsolutepath(),suffix); } } } } } |
2.刪除文件夾下某個格式的所有文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package file; import java.io.file; public class demo3 { public static void main(string[] args) { // todo auto-generated method stub delete( "d:/a" , ".jpg" ); } public static void delete(string path,string suffix) { file f = new file(path); if (f.isfile()) { if (f.getname().endswith(suffix)) { system.out.println(f.getabsolutepath()+ "成功刪除" ); f.delete(); } } else { file[] files = f.listfiles(); if (files!= null &&files.length> 0 ) { for (file file : files) { delete(file.getabsolutepath(),suffix); } } } } } |
總結
以上所述是小編給大家介紹的java實現輸出文件夾下某個格式的所有文件實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/qq_32539825/article/details/80693901