zoukankan      html  css  js  c++  java
  • java简单实现搜索指定后缀文件

     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 }

    代码很简单。核心就是一个递归处理

  • 相关阅读:
    Stack frame
    How a stack frame works 栈帧的要素与构建步骤
    Symbol Table
    函数调用栈实例研究
    崩溃日志的实例
    符号表的作用和地位
    A C compiler that parses this code will contain at least the following symbol table entries
    前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线。
    Thinkphp5+PHPExcel实现批量上传表格数据功能
    PHP 开发API接口签名验证
  • 原文地址:https://www.cnblogs.com/dzxczx666/p/8585795.html
Copyright © 2011-2022 走看看