java獲取一個目錄下的所有的檔案或資料夾名稱
阿新 • • 發佈:2019-01-09
public static void main(String[] args) { testFileDirOrName("你的專案路徑"); } private static void testFileDirOrName(String path) { File dirFile = new File(path); if (dirFile.exists()) { File[] files = dirFile.listFiles(); if (files != null) { for (File fileChildDir : files) { //輸出檔名或者資料夾名System.out.print(fileChildDir.getName()); if (fileChildDir.isDirectory()) { System.out.println(" : 此為目錄名"); //通過遞迴的方式,可以把目錄中的所有檔案全部遍歷出來 testFileDirOrName(fileChildDir.getAbsolutePath()); } if(fileChildDir.isFile()) { System.out.println(" : 此為檔名"); } } } }else{ System.out.println("你想查詢的檔案不存在"); } }
測試結果如下: .classpath : 此為檔名 .idea : 此為目錄名 encodings.xml : 此為檔名 kotlinc.xml : 此為檔名 libraries : 此為目錄名 bss.xml : 此為檔名 ehcache.xml : 此為檔名 hdiv.xml : 此為檔名 ibatis_2_3_2.xml : 此為檔名 j2ee.xml : 此為檔名 jakarta_commons.xml : 此為檔名 lib.xml : 此為檔名 ojdbc14.xml : 此為檔名 poi_3_0_alpha2_20060616.xml : 此為檔名 。。。。。。。