Github项目地址:https://github.com/softwareCQT/web_camp/tree/master/wordCount

一、题目描述

  • 实现一个简单而完整的软件工具(源程序特征统计程序)。

  • 进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。

  • 进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。


二、WC 项目要求

  • wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

  • 实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。

  • 具体功能要求:程序处理用户需求的模式为:wc.exe [parameter] [file_name]


三、核心代码

  • 文件处理(包括处理通配符*?和-s递归)

    /**
    * @author chenqiting
    */
    public class FileUtil {

       /***
        * 判断文件是否合法 且 处理通配符并返回文件列表
        * @return List<File>
        */
       public static List<File> accessRegx(String fileName){
           if (fileName.contains(CommandConstants.UNIVERSAL_CHAR_ONE)
                   || fileName.contains(CommandConstants.UNIVERSAL_CHAR_TWO)) {
             //判断是否存在通配符,统一换掉参数
               fileName = fileName.
                       replace(CommandConstants.UNIVERSAL_CHAR_TWO, CommandConstants.UNIVERSAL_CHAR_ONE);
               //如果是绝对路径,获取绝对路径的前半段,即获取到*号之前的路径
               int index = fileName.indexOf("*");
               //标志文件是否在文件后缀加的通配符
               boolean flag = (index == fileName.length() - 1);
               String parentDirectory;
               String childFileName;
               //如果是文件类型通配符,父路径需要重新处理
               if (flag) {
                   index = fileName.lastIndexOf("\\");
                   index = index == -1 ? 0 : index;
              }
               parentDirectory = fileName.substring(0, index);
               childFileName = fileName.substring(index);
               //系统路径匹配器
               PathMatcher pathMatcher;
               File file;
               //空字符串表示需要当前路径匹配
               if ("".equals(parentDirectory)){
                   file = new File(".");
                   String string = file.getAbsolutePath().replace(".", "").replace("\\", "\\\\");

                   file = new File(string);
                   pathMatcher = FileSystems.getDefault().
                           getPathMatcher("regex:" + string + "\\\\" + childFileName);
              }else {
                   parentDirectory = parentDirectory.replace("\\", "\\\\");
                   file = new File(parentDirectory);
                   //在parentDirectory目录下进行遍历
                   pathMatcher = FileSystems.getDefault().
                           getPathMatcher("glob:" + parentDirectory + childFileName);
              }
               return stackForFile(file, pathMatcher);
          }else {
               File file = new File(fileName);
               //判断文件是否存在
               if (file.exists()){
                   if (file.isDirectory()){
                       System.out.println(file.getName() + "不是文件,请重新输入");
                  }
              }else {
                   System.out.println("找不到该文件");
              }
               
版权声明:本文为chenqiting原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chenqiting/p/12554114.html