日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取指定文件夹下的所有文件名

發布時間:2025/4/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取指定文件夹下的所有文件名 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遞歸獲取某目錄下的所有文件名,路徑。

1 package test; 2 3 import java.io.File; 4 5 public class GetFileName{ 6 7 public static void main(String[] args) { 8 // This is the path where the file's name you want to take. 9 String path = "C:\\ProgramData"; 10 getFile(path); 11 } 12 13 private static void getFile(String path) { 14 // get file list where the path has 15 File file = new File(path); 16 // get the folder list 17 File[] array = file.listFiles(); 18 19 if (array != null) //沒權限訪問,則會報錯文件為null 20 for (int i = 0; i < array.length; i++) { 21 if (array[i].isFile()) {    //可以這樣判斷?if?(obj?instanceof?File)?{ ? 22 // only take file name 23 System.out.println("^^^^^" + array[i].getName()); 24 // take file path and name 25 System.out.println("#####" + array[i]); 26 // take file path and name 27 System.out.println("*****" + array[i].getPath()); 28 } else if (array[i] instanceof File) { 29 getFile(array[i].getPath()); 30 } 31 } 32 } 33 }

若要返回這些東西,

則:

package test;import java.io.File;/*** * 獲取指定目錄下的所有的文件(不包括文件夾),采用了遞歸 * * @param obj * @return */ public static ArrayList<File> getListFiles(Object obj) { File directory = null; if (obj instanceof File) { directory = (File) obj; } else { directory = new File(obj.toString()); } ArrayList<File> files = new ArrayList<File>(); if (directory.isFile()) { files.add(directory); return files; } else if (directory.isDirectory()) { File[] fileArr = directory.listFiles(); for (int i = 0; i < fileArr.length; i++) { File fileOne = fileArr[i]; files.addAll(getListFiles(fileOne)); } } return files; }

參考:  http://blog.csdn.net/tomorrowzm/article/details/3693653?

http://hw1287789687.iteye.com/blog/1946488?

轉載于:https://www.cnblogs.com/liu-qing/p/3930955.html

總結

以上是生活随笔為你收集整理的获取指定文件夹下的所有文件名的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。