十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
可以獲得文件夾中所有文件的路徑及文件名。
創(chuàng)新互聯(lián)公司主營(yíng)武穴網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開(kāi)發(fā),武穴h5小程序制作搭建,武穴網(wǎng)站營(yíng)銷(xiāo)推廣歡迎武穴等地區(qū)企業(yè)咨詢
代碼很簡(jiǎn)單,直接上車(chē),車(chē)上再解釋?zhuān)?/p>
/** * 獲取指定目錄內(nèi)所有文件路徑 * @param dirPath 需要查詢的文件目錄 * @param _type 查詢類(lèi)型,比如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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。