项目相关要求
GitHub地址:https://github.com/3216004716/WC
实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
序处理用户需求的模式为:
wc.exe [parameter] [file_name]
基本功能列表:
wc.exe -c file.c //返回文件 file.c 的字符数
wc.exe -w file.c //返回文件 file.c 的词的数目
wc.exe -l file.c //返回文件 file.c 的行数
扩展功能:
-s 递归处理目录下符合条件的文件。
-a 返回更复杂的数据(代码行 / 空行 / 注释行)。
空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
代码行:本行包括多于一个字符的代码。
注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:
高级功能:
-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。
返回当前目录及子目录中所有*.c 文件的代码行数、空行数、注释行数。
解题思路
程序要实现统计文件中信息特征的功能,则要用FileReader类来读取文件中的内容,再通过BufferedReader和StringReader转换成对应的io流,运用方法来统计特征数据。获取文件行数、字符数、词数分别创建getLineNum()、getCharNum()、getWordNum()方法,封装在Method类中,再另外创建一个WCTest类,用main方法运行。
遇到的困难及解决方法
- 遇到困难:Ⅰ. 对IO流的运用不熟练 Ⅱ. 不知道如何判断一段字符串中有多少单词 Ⅲ:main函数里的args数组的含义
- 做过尝试:翻阅了Java基础教程书籍有关IO流的内容,并上网搜索了判断一个单词的方法
- 是否解决:通过查书和网上搜索解决了变成问题
- 有何收获:了解了FileReader、BufferedReader和StringReader的用法、并通过对比更加了解四大IO流之间的区别;学习到了通过使用Pattern类、Matcher类和正则表达式来对目标字符串展开匹配检测;懂得了main函数里args数组的含义和用法
关键代码or设计说明
- 设计
- -w 的实现方法
1 public void getWordNum(String Path) { 2 //创建方法,统计文件的单词数 3 int word = 0; 4 String string = null; 5 StringBuffer sBuffer = new StringBuffer(); 6 try { 7 File file = new File(Path); 8 FileReader frReader = new FileReader(file); 9 BufferedReader bfr = new BufferedReader(frReader); 10 while ( (string = bfr.readLine()) != null ) { 11 sBuffer.append(string+"\n"); 12 } 13 frReader.close(); 14 bfr.close(); 15 } catch (Exception e) { 16 System.out.println("该文件不存在!"); 17 } 18 Pattern p = Pattern.compile("\\b[A-Za-z]+\\b");//创建以字母为开头或结尾的模板 19 Matcher m = p.matcher(sBuffer.toString()); 20 while (m.find()) { 21 word++; 22 } 23 System.out.println("该文件词的个数为:"+word); 24 }
- -c 的实现方法
1 public void getCharNum(String Path) { 2 //创建方法,获取文件的字符数 3 long length = 0; 4 String string = null; 5 try { 6 File file = new File(Path); 7 FileReader frReader = new FileReader(file); 8 BufferedReader bfr = new BufferedReader(frReader); 9 while ((string = bfr.readLine())!=null) { 10 length += string.length(); 11 } 12 bfr.close(); 13 frReader.close(); 14 System.out.println("该文件的字符数为:"+length); 15 } catch (Exception e) { 16 System.out.println("文件不存在!"); 17 } 18 }
- -l 的实现方法
1 public void getLineNum(String Path) { 2 //创建方法,获取文件的行数 3 int line = 0; 4 try { 5 File file = new File(Path); 6 FileReader fReader = new FileReader(file); 7 BufferedReader bfr = new BufferedReader(fReader); 8 while (bfr.readLine() != null) { 9 line++; 10 } 11 fReader.close(); 12 bfr.close(); 13 } catch (Exception e) { 14 System.out.println("文件不存在!"); 15 } 16 System.out.println("该文件的行数是:"+line); 17 }
- WCTest类中的main方法
1 public static void main(String[] args) throws IOException { 2 3 switch (args[0]) { 4 case "-c": 5 Method method = new Method(); 6 method.getCharNum(args[1]); 7 break; 8 9 case "-l": 10 Method method1 = new Method(); 11 method1.getLineNum(args[1]); 12 break; 13 14 case "-w": 15 Method iMethod = new Method(); 16 iMethod.getWordNum(args[1]); 17 break; 18 19 default: 20 System.out.println("输入功能错误,请重新选择!"); 21 break; 22 } 23 24 }
测试结果
代码覆盖率
PSP
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 60 | 30 |
· Estimate | · 估计这个任务需要多少时间 | 60 | 30 |
Development | 开发 | 940 | 1135 |
· Analysis | · 需求分析 (包括学习新技术) | 360 | 300 |
· Design Spec | · 生成设计文档 | 30 | 45 |
· Design Review | · 设计复审 (和同事审核设计文档) | 30 | 20 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 10 | 5 |
· Design | · 具体设计 | 60 | 45 |
· Coding | · 具体编码 | 300 | 500 |
· Code Review | · 代码复审 | 30 | 40 |
· Test | · 测试(自我测试,修改代码,提交修改) | 120 | 180 |
Reporting | 报告 | 150 | 110 |
· Test Report | · 测试报告 | 60 | 30 |
· Size Measurement | · 计算工作量 | 30 | 20 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 60 | 60 |
合计 | 1150 | 1275 |
总结
统计行数和字符数的实现比较简单,但词的个数是通过学习了pattern类和machter类来统计的,复习了以前学过但还是有点模糊的知识,又学到了新的东西。做完这个程序才发现自己的编程能力还是有很多欠缺的地方,以后还要继续练习,逐渐熟练,越做越好。