Android 遍历文件夹中所有文件的实例代码
可以获得文件夹中所有文件的路径及文件名。
代码很简单,直接上车,车上再解释:
/**
*获取指定目录内所有文件路径
*@paramdirPath需要查询的文件目录
*@param_type查询类型,比如mp3什么的
*/
publicstaticJSONArraygetAllFiles(StringdirPath,String_type){
Filef=newFile(dirPath);
if(!f.exists()){//判断路径是否存在
returnnull;
}
File[]files=f.listFiles();
if(files==null){//判断权限
returnnull;
}
JSONArrayfileList=newJSONArray();
for(File_file:files){//遍历目录
if(_file.isFile()&&_file.getName().endsWith(_type)){
String_name=_file.getName();
StringfilePath=_file.getAbsolutePath();//获取文件路径
StringfileName=_file.getName().substring(0,_name.length()-4);//获取文件名
//Log.d("LOGCAT","fileName:"+fileName);
//Log.d("LOGCAT","filePath:"+filePath);
try{
JSONObject_fInfo=newJSONObject();
_fInfo.put("name",fileName);
_fInfo.put("path",filePath);
fileList.put(_fInfo);
}catch(Exceptione){
}
}elseif(_file.isDirectory()){//查询子目录
getAllFiles(_file.getAbsolutePath(),_type);
}else{
}
}
returnfileList;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下