Java 之遞迴遍曆目錄

來源:互聯網
上載者:User

標籤:abs   名稱   listdir   exist   import   else   絕對路徑   name   ram   

Java 之遞迴遍曆目錄

一、內容

  輸出指定目錄(檔案夾)下的所有檔案(包括目錄)的絕對路徑

二、原始碼:RecursiveListDirectory.java

 1 package cn.com.zfc.day016; 2  3 import java.io.File; 4  5 /** 6  * @describe 遞迴遍曆目錄 7  * @author zfc 8  * @date 2018年1月1日 上午8:44:55 9  */10 public class RecursiveListDirectory {11 12     public static void main(String[] args) {13         String directoryName = "F:\\StudySoft";14         // 1、映射目錄檔案15         File directory = new File(directoryName);16         // 2、調用方法17         recursiveListDirectory(directory);18     }19 20     /**21      * 輸出指定目錄下面的檔案名稱,包括子目錄22      * 23      * @param directoryName:目錄名稱24      * @author zfc25      * @date 2018年1月1日上午8:46:0826      *27      */28     public static void recursiveListDirectory(File directory) {29         // 1、判斷映射的目錄檔案是否存在?30         if (!directory.exists()) {31             // 不存在則直接返回32             return;33         }34         // 2、判斷是否是目錄?35         if (!directory.isDirectory()) {36             // 不是目錄,判斷是否是檔案?37             if (directory.isFile()) {38                 System.out.println("檔案絕對路徑:" + directory.getAbsolutePath());39             }40         } else {41             // 是目錄,擷取該目錄下面的所有檔案(包括目錄)42             File[] files = directory.listFiles();43             // 判斷 files 是否為空白?44             if (null != files) {45                 // 遍曆檔案數組46                 for (File f : files) {47                     // 判斷是否是目錄?48                     if (f.isDirectory()) {49                         // 是目錄50                         System.out.println("目錄絕對路徑:" + f.getAbsolutePath());51                         recursiveListDirectory(f);52                     } else {53                         // 不是目錄,判斷是否是檔案?54                         if (f.isFile()) {55                             System.out.println("檔案絕對路徑:" + f.getAbsolutePath());56                         }57                     }58                 }59             }60         }61     }62 }

三、運行結果

Java 之遞迴遍曆目錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.