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);
}
// 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);
}