Java遍歷文件夾及子目錄代碼實(shí)例
主要邏輯
使用scanner類獲取輸入的目錄,并創(chuàng)建文件對(duì)象。
新建一個(gè)遍歷文件夾的方法,參數(shù)是已創(chuàng)建的文件對(duì)象,遞歸調(diào)用自己。
import java.io.File;public class BianLi{ public static void huoQuMuLu(File a) { File[] fd = a.listFiles(); //獲取目錄數(shù)組 for(int i=0;i<fd.length;i++){ //將文件對(duì)象數(shù)組轉(zhuǎn)換為字符數(shù)組,并輸出數(shù)組 System.out.println(fd[i]); if(fd[i].isDirectory()){ //判斷是不是目錄huoQuMuLu(fd[i]); //遞歸 調(diào)用自己 } } //return fd[i]; } public static void main(String args[]) { String str2 = ''; System.out.println('輸入需要遍歷的文件夾'); Scanner scan = new Scanner(System.in); //獲取鍵盤輸入數(shù)據(jù) if(scan.hasNextLine()){ //判斷scan有沒(méi)有數(shù)據(jù) str2 = scan.nextLine(); //獲取輸入的地址 System.out.println('開(kāi)始遍歷'+str2+'n'); } scan.close(); File f1 = new File(str2); huoQuMuLu(f1); } }
測(cè)試
成功!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于 Python 實(shí)踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. Python獲取B站粉絲數(shù)的示例代碼4. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效5. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題6. Python 中如何使用 virtualenv 管理虛擬環(huán)境7. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)8. python利用opencv實(shí)現(xiàn)顏色檢測(cè)9. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖10. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車
