读取目录及子目录下指定文件名的路径


import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* 读取目录及子目录下指定文件名的路径 并放到一个数组里面返回遍历
* @author
*
*/
public class FileViewer {
//public static

/**
*
* @param path 文件路径
* @param suffix 后缀名
* @param isdepth 是否遍历子目录
* @return
*/
public static int getListFiles(List<String> filelist,String path, String suffix, int maxDepth,boolean incSubDirs)
{
File file = new File(path);
int curDepth=0;
//int filecount=0;
return FileViewer.listFile(filelist,file ,suffix, curDepth,maxDepth,incSubDirs);
}

private static int listFile(List<String> filelist,File f, String suffix,int curDepth,int maxDepth,boolean incSubDirs)
{
//是目录,同时需要遍历子目录
int totalCount=0;
if (f.isDirectory())
{
if(curDepth<maxDepth||maxDepth<=0)
{
File[] t = f.listFiles();
for (int i = 0; i < t.length; i++)
{
if(incSubDirs&&t[i].isDirectory())
{
filelist.add(t[i].getAbsolutePath());
totalCount++;
}
totalCount+=listFile(filelist,t[i], suffix, curDepth+1,maxDepth,incSubDirs);
}
}
}
else
{
String filePath = f.getAbsolutePath();


//System.out.println("suffix = "+suffix);
if("".equals(suffix)|| suffix == null)
{
//后缀名为null则为所有文件
//System.out.println("----------------");
filelist.add(filePath);
totalCount++;
}
else
{
int begIndex = filePath.lastIndexOf(".");//最后一个.(即后缀名前面的.)的索引
String tempsuffix = "";

if(begIndex != -1)//防止是文件但却没有后缀名结束的文件
{
tempsuffix = filePath.substring(begIndex + 1, filePath.length());
}

if(tempsuffix.equals(suffix))
{
filelist.add(filePath);
totalCount++;
}
//System.out.println("|||||||||||||||||||");
}

}

return totalCount;
}
public static void main(String[] argv){
String f = ("E:\\新建文件夹 (4)\\trunk\\02 开发库\\0204.编码\\NetEyeVPNClient");
String java=null;
FileViewer aa=new FileViewer();
List<String> fileList = new ArrayList<String>();
int count=aa.getListFiles(fileList,f,java, 0,false);
for(int i=0;i<fileList.size();i++)
{
System.out.println(fileList.get(i));
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值