久久96国产精品久久久-久久发布国产伦子伦精品-久久精品国产精品青草-久久天天躁夜夜躁狠狠85麻豆

技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

Android如何遍歷文件夾中所有文件的實(shí)例代碼

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-07-07 00:25┆點(diǎn)擊:

可以獲得文件夾中所有文件的路徑及文件名。

代碼很簡(jiǎn)單,直接上車,車上再解釋:

/** * 獲取指定目錄內(nèi)所有文件路徑 * @param dirPath 需要查詢的文件目錄 * @param _type 查詢類型,比如mp3什么的 */ public static JSONArray getAllFiles(String dirPath, String _type) { File f = new File(dirPath); if (!f.exists()) {//判斷路徑是否存在 return null; } File[] files = f.listFiles(); if(files==null){//判斷權(quán)限 return null; } JSONArray fileList = new JSONArray(); for (File _file : files) {//遍歷目錄 if(_file.isFile() && _file.getName().endsWith(_type)){ String _name=_file.getName(); String filePath = _file.getAbsolutePath();//獲取文件路徑 String fileName = _file.getName().substring(0,_name.length()-4);//獲取文件名 // Log.d("LOGCAT","fileName:"+fileName); // Log.d("LOGCAT","filePath:"+filePath); try { JSONObject _fInfo = new JSONObject(); _fInfo.put("name", fileName); _fInfo.put("path", filePath); fileList.put(_fInfo); }catch (Exception e){ } } else if(_file.isDirectory()){//查詢子目錄 getAllFiles(_file.getAbsolutePath(), _type); } else{ } } return fileList; }