您的位置:首頁技術文章
文章詳情頁

Java遍歷文件夾及子目錄代碼實例

瀏覽:10日期:2022-08-28 14:37:03

主要邏輯

使用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); } }

測試

Java遍歷文件夾及子目錄代碼實例

成功!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
国产综合久久一区二区三区