Java遍歷文件夾及子目錄代碼實例
主要邏輯
使用scanner類獲取輸入的目錄,并創建文件對象。
新建一個遍歷文件夾的方法,參數是已創建的文件對象,遞歸調用自己。
import java.io.File;public class BianLi{ public static void huoQuMuLu(File a) { File[] fd = a.listFiles(); //獲取目錄數組 for(int i=0;i<fd.length;i++){ //將文件對象數組轉換為字符數組,并輸出數組 System.out.println(fd[i]); if(fd[i].isDirectory()){ //判斷是不是目錄huoQuMuLu(fd[i]); //遞歸 調用自己 } } //return fd[i]; } public static void main(String args[]) { String str2 = ''; System.out.println('輸入需要遍歷的文件夾'); Scanner scan = new Scanner(System.in); //獲取鍵盤輸入數據 if(scan.hasNextLine()){ //判斷scan有沒有數據 str2 = scan.nextLine(); //獲取輸入的地址 System.out.println('開始遍歷'+str2+'n'); } scan.close(); File f1 = new File(str2); huoQuMuLu(f1); } }
測試
成功!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. React+umi+typeScript創建項目的過程2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析3. SharePoint Server 2019新特性介紹4. ASP中常用的22個FSO文件操作函數整理5. 三個不常見的 HTML5 實用新特性簡介6. ASP調用WebService轉化成JSON數據,附json.min.asp7. .Net core 的熱插拔機制的深入探索及卸載問題求救指南8. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁9. 讀大數據量的XML文件的讀取問題10. 解決ASP中http狀態跳轉返回錯誤頁的問題
