我写的例子有点简单,不过能解决问题,看看行不,有问题告我- import java.io.File;
- public class Test_7_31 {
- //这里标记符合文件的个数
- private static int x = 0;
- public static void main(String[] args) {
- //设置,你明白
- String extension = "jpg";
- String pathName ="E:/ww";
- //File file = new File("E:/ww/");
- search(pathName,extension);
- System.out.println(x);
- }
-
-
- private static void search(String pathName,String fileName) {
- File file = new File(pathName);
- String str[] = file.list();
- File directoryName =null;
- for(String s:str){
- //检测directoryName是否以/为结尾(系统分隔符)
- if(pathName.endsWith(File.separator)){
- directoryName = new File(pathName+s);
- }
-
- else{
- directoryName = new File(pathName+File.separator+s);
- }
-
- //若果directoryName是一个文件并且扩展名为fileName x++
- int i = s.lastIndexOf('.');
- if(directoryName.isFile()&&s.substring(i+1).equalsIgnoreCase(fileName))
- {
- x++;
- }
-
- //检测子目录
- if(directoryName.isDirectory()){
- search(directoryName.toString(),fileName);
- }
- }
- }
- }
复制代码 [ 本帖最后由 李龙 于 2011-07-31 21:25 编辑 ] |