android 遍历找文件

本文介绍如何在一个目录中查找以指定前缀命名的一系列文件,并遍历目录直到找到特定文件为止。包括文件查找与目录遍历的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在一个目录下找指定文件:videoename为文件名,如test.rmvb,subtitlePath=/mnt/sdb/adb1/ 查找结果:test.mkv等一系列的以test开头的文件
// add by cg 2013 07 25 for Subtitle
public boolean IsFindSubtitle(String videoename, String subtitlePath) {
String subtitlename = videoename.substring(0,
videoename.lastIndexOf("."));
File filePath = new File(subtitlePath);
if (filePath == null) {
return false;
}
// System.out.println("=====================IsFindSubtitle===================");
if (filePath.isDirectory()) {


File[] fileArray = filePath.listFiles();


if (fileArray != null) {


for (int i = 0; i < fileArray.length; i++) {


if (fileArray[i].getName().length() > subtitlename.length()) {


if (fileArray[i].getName()
.substring(0, subtitlename.length())
.equals(subtitlename)
&& (!fileArray[i].getName().equals(videoename))) {
// System.out.println(fileArray[i].getPath()+
// fileArray[i].getName());
Log.v("==========chenguo=====22222222222====",
"fileArray[" + i + "].getPath()"
+ fileArray[i].getPath()
+ " fileArray[" + i + "].getName()"
+ fileArray[i].getName());


}
}


}


}
}
return false;


}
// end by cg


2.遍历目录,直到找"Q-Agingtest.ts"文件为止
// 查找testvideo.rmvb
private void Traversaltestvideo(File f) {
int flage = 0;
if (f != null && false == testvideoinfo[1].equals("Q-Agingtest.ts")) {// f为null或找到testvideo.rmvb退出递归


if (f.isDirectory()) {


File[] fileArray = f.listFiles();


if (fileArray != null) {


for (int i = 0; i < fileArray.length; i++) {


// 递归调用
// System.out.println(fileArray[i].getName());
Traversaltestvideo(fileArray[i]);
}


}


}


else {


if (1 != flage) {
String testvideo = "Q-Agingtest.ts";
if (f.getName().equals(testvideo)) {
flage = 1;
testvideoinfo[0] = f.getPath();
testvideoinfo[1] = f.getName();
// Log.v("====testvideoinfo===","testvideoinfo testvideoinfo ok "
// + testvideoinfo[0]);
System.out.println(f.getPath());


}
}
}


}


}
测试:在/mnt/目录开始遍历所有目录及文件,直到找到"Q-Agingtest.ts"或全部遍历完为止
void test()
{
String[] testvideoinfo = new String[2];
String fileName = "/mnt" + File.separator;
File f = new File(fileName);
// 初始化uxb_numinfo 0
for (int i = 0; i < 2; i++)
testvideoinfo[i] = "0";
Traversaltestvideo(f);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值