WordCount的实现(java)
一.Gitee地址:
链接:https://gitee.com/mmmmx/WordCount/tree/master
二.项目分析:
WordCount功能为统计文件中信息,主要涉及的知识为文件IO流和对字符串的处理。我选择熟悉的java来完成此项目。
一开始看程序预期运行效果和过程,对命令模式启动程序一无所知,百度后发现args中即存储的命令行。
程序测试的时候问题又来了,所需要的args参数该如何加入?百度后又得知在运行配置和调试配置中可以设置,在此,解决了绝大多数问题。
三.PSP表格:
psp2.1 | psp阶段 |
估计耗时(分钟) |
实际耗时(分钟) |
Planning | 计划 | 40 | 20 |
· Estimate | · 估计这个任务需要多少时间 | 40 | 20 |
Development | 开发 | 490 | 450 |
· Analysis | · 需求分析 (包括学习新技术) | 30 | 30 |
· Design Spec | · 生成设计文档 | 60 | 50 |
· Design Review | · 设计复审 (和同事审核设计文档) | 60 | 90 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 10 | 5 |
· Design | · 具体设计 | 50 | 55 |
· Coding | · 具体编码 | 130 | 100 |
· Code Review | · 代码复审 | 60 | 60 |
· Test | · 测试(自我测试,修改代码,提交修改) | 90 | 60 |
Reporting | 报告 | 60 | 70 |
· Test Report | · 测试报告 | 40 | 40 |
· Size Measurement | · 计算工作量 | 10 | 5 |
· Postmortem & ProcessImprovement Plan |
· 事后总结, 并提出过程改进计划 | 10 | 25 |
合计 | 590 | 540 |
四.代码展示:
WordCount功能为统计文件中信息,则按照功能需求依次先完成各种数据的统计,以下是具体功能实现代码
所有对文件的统计方法写在此工具类中
涉及到的知识:对文件IO流的操作,对字符串的操作
以下是此类总览,详细见后面,具体完整代码点最上面码云链接
1 import java.io.File; 2 import java.io.InputStreamReader; 3 import java.util.ArrayList; 4 import java.io.BufferedReader; 5 import java.io.BufferedWriter; 6 import java.io.FileInputStream; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 10 11 public class WordCount 12 { 13 14 public static String inputFile; 15 public static String outputFile; 16 public static int char1; 17 public static int word1; 18 public static int line1; 19 public static boolean need1; 20 public static boolean need2; 21 public static boolean need3; 22 public static boolean need4;
主函数及其类中方法如下:
public static void main(String[] args) { inputFile=""; for(int i=0;i<args.length;i++) { if ("-c".equals(args[i])) { need1 = true; } else if ("-w".equals(args[i])) { need2 = true; } else if ("-l".equals(args[i])) { need3 = true; } else if ("-o".equals(args[i])) { need4 = true; outputFile = args[i + 1]; } else { if (!args[i - 1].equals("-e") && !args[i - 1].equals("-o")) { inputFile = args[i]; } } } String outputStr=""; ArrayList<String> fileNames =new ArrayList<String>(); fileNames.add(inputFile); int len=fileNames.size(); String fn; for(int i=0;i<len;i++) { fn=fileNames.get(i); String fileShortName=fn.substring(fn.lastIndexOf("\\")+1, fn.length()); if(need1||need2||need3) { getBasicInfo(fn); if(need1) { outputStr+=fileShortName; outputStr+=", char: "; outputStr+=char1; outputStr+="\r\n"; } if(need2) { outputStr+=fileShortName; outputStr+=", word: "; outputStr+=word1; outputStr+="\r\n"; } if(need3) { outputStr+=fileShortName; outputStr+=", line: "; outputStr+=line1; outputStr+="\r\n"; } } } if(!need4) { outputFile="result.txt"; } try { File writename = new File(outputFile); writename.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write(outputStr); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
下面是其他方法的关键代码:
public static void getBasicInfo(String fileName) { char1=0; word1=0; line1=0; char charNow; try { File filename = new File(fileName); InputStreamReader reader = new InputStreamReader( new FileInputStream(filename)); BufferedReader br = new BufferedReader(reader); String line ; line = br.readLine(); while (line != null) { char1+=line.length(); line1++; boolean partition = true; for(int i=0;i<line.length();i++) { charNow=line.charAt(i); if(partition==true&&charNow!=' '&&charNow!='\t'&&charNow!=','&&charNow!=',') { word1++; } if(charNow==' '||charNow=='\t'||charNow==','||charNow==',') { partition=true; } } line = br.readLine(); } char1+=line1-1; br.close(); } catch (IOException e) { e.printStackTrace(); } }
五.测试
具体设计的测试用例如下:
测试用例
测试结果
测试评价
基本功能大体已经实现,测试结果和预想中的一样,效果还不错,比较令人满意
六.参考文献:
https://www.cnblogs.com/YuQiao0303/p/8607697.html