1 package sb_eclipse; 2 import java.io.File; 3 import java.io.IOException; 4 import java.util.ArrayList; 5 6 public class ScanSuffix { 7 private static int ScanSuffixCount = 0; 8 private static ArrayList<String> ScanSuffixFiles = new ArrayList<String>(); 9 10 11 public static void ScanSuffix(String path, String suffix) { 12 File file,tempFile; 13 file = new File(path); 14 if (! file.isDirectory()) { 15 return ; 16 } 17 if (suffix.isEmpty()) { 18 return ; 19 } 20 21 String[] fileList = file.list(); 22 for(String fileName : fileList) { 23 tempFile = new File(path + "\" + fileName); 24 if (tempFile.isDirectory()) { 25 ScanSuffix(path + "\" + fileName, suffix); 26 } 27 if (tempFile.isFile()) { 28 String[] splitFileName = fileName.split("\."); 29 String ext = splitFileName[splitFileName.length - 1]; 30 if (ext.equals(suffix)) { 31 ScanSuffixCount += 1; 32 ScanSuffixFiles.add(path + "\" + fileName); 33 } 34 } 35 } 36 } 37 38 public static void main(String[] args) { 39 // TODO Auto-generated method stub 40 String path = "F:\谷歌下载"; 41 String suffix = "php"; 42 System.out.println("开始查找..."); 43 long beginTime = System.currentTimeMillis()/1000; 44 ScanSuffix(path, suffix); 45 long endTime = System.currentTimeMillis()/1000; 46 long time = endTime - beginTime; 47 for (String fileName : ScanSuffixFiles) { 48 System.out.println(fileName); 49 } 50 System.out.println("查找结束,耗时" + Long.toString(time) + "秒"); 51 System.out.println("在" + path + "目录下搜索后缀为" + suffix + "的数目有" + ScanSuffixCount + "个"); 52 53 } 54 55 }
代码很简单。核心就是一个递归处理