標籤:pat dir getname catch too except cat ons name
public static String getAllDirectorisAndFiles(String path){ Map<String, Object> responseMap = new HashMap<String, Object>();responseMap.put("time_stamp", Tools.currentTime());String responseString = "";try {getDirectorisAndFiles(path, responseMap, "data");responseString = (new Gson()).toJson(responseMap);} catch (Exception e) {System.out.println("files列表讀取異常");}return responseString; } public static void getDirectorisAndFiles(String path, Map<String, Object> resultMap, String directoryName) { System.out.println(path); System.out.println(directoryName); File file = new File(path); ArrayList<Object> dataArray = new ArrayList<Object>(); File [] files = file.listFiles(); for(File a:files) { if(a.isDirectory()) { Map<String, Object> tempMap = new HashMap<String, Object>(); getDirectorisAndFiles(path + a.getName() + "/", tempMap, a.getName()); dataArray.add(tempMap); }else{ dataArray.add(a.getName()); } } resultMap.put(directoryName, dataArray);}
遞迴讀取制定目錄下所有檔案夾和檔案的實現(java)