1. 程式人生 > >java 獲取路徑下所有檔案

java 獲取路徑下所有檔案

思路:通過遞迴遍歷資料夾

	public static void getFiles(List<File>fileList, String path){
		try {
			File file = new File(path);
			if(file.isDirectory()){
				File []files = file.listFiles();
				for(File fileIndex:files){
					//如果這個檔案是目錄,則進行遞迴搜尋
					if(fileIndex.isDirectory()){
						getFiles(fileList,fileIndex.getPath());
					}else {
						//如果檔案是普通檔案,則將檔案控制代碼放入集合中
						fileList.add(fileIndex);
					}
				}
			}
		}catch (Exception e){

		}
	}