第一次作业-java实现WordCount基本功能
码云项目地址:
https://gitee.com/x2yx/WordCount
PSP表格
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
Planning |
计划 |
30 |
40 |
· Estimate |
· 估计这个任务需要多少时间 |
30 |
45 |
Development |
开发 |
1090 |
1580 |
· Analysis |
· 需求分析 (包括学习新技术) |
100 |
200 |
· Design Spec |
· 生成设计文档 |
50 |
100 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
40 |
60 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
20 |
30 |
· Design |
· 具体设计 |
50 |
100 |
· Coding |
· 具体编码 |
600 |
800 |
· Code Review |
· 代码复审 |
30 |
40 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
200 |
250 |
Reporting |
报告 |
120 |
150 |
· Test Report |
· 测试报告 |
80 |
100 |
· Size Measurement |
· 计算工作量 |
20 |
20 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
15 |
20 |
|
合计 |
1385 |
1955 |
解题思路
刚看到这个题目的时候觉得有些无从下手,统计字符与行数,感觉需要用到文件的输入输出知识,然后按行读取,对特殊字符进行一些判断,然后进行计数。
所以我学习了网上关于java IO流的相关知识,以及参考了部分代码。
设计实现思路
以Main().java为入口,通过cmd传入参数到main(String[] args)获取操作和文件路径,getFiles()获取需要符合条件的文件,将其绝对路径添加到ArrayList中,最后遍历
ArrayList并根据相应的操作对相应的文件执行相应的操作方法。最后将词频统计的结果输出到指定目录的TXT文件中。该程序共设计两个类Main.java、CountUtils.java。
Main.java类主要处理传入的参数并根据参数执行CountUtils.java里面的功能函数,CountUtils.java中主要为各类功能函数和输出函数。
代码说明
Main.java,通过args获取要执行的操作,以及读取文件目录
import java.io.File; import java.io.IOException; import java.util.ArrayList; public class Main { private static boolean countChar = false; private static boolean countWord = false; private static boolean countLine = false; // private static boolean countOut = false; //利用ArrayList存储符合条件的文件的绝对路径 private static ArrayList<String> fileList = new ArrayList<String>(); public static void main(String[] args) throws IOException,ArrayIndexOutOfBoundsException { //默认最后一个参数为路径名 String path = args[args.length - 1]; //System.out.println(path); CountUtils cUtils = new CountUtils(); //判断需要执行的功能 for(int i = 0; i < args.length - 1; i++) { if(args[i].equals("-c")) { countChar = true; } if(args[i].equals("-w")) { countWord = true; } if(args[i].equals("-l")) { countLine = true; } if(args[i].equals("-o")) { //countOut = true; } } //获取目录名 String paths[] = path.split("\\\\"); StringBuilder sb = new StringBuilder(); for(int i=0;i<paths.length-1;i++) { if(i==paths.length-2) { sb.append(paths[i]); }else { sb.append(paths[i]+"\\"); } } String dirName = sb.toString(); File file = new File(dirName); if(!file.isDirectory()) { System.out.println("路径错误!"); } String fileName =paths[paths.length-1]; //对文件名通配符处理 fileName = fileName.replaceAll("\\*", "\\.+").replaceAll("\\?", "\\."); //获取当前目录下符合条件的文件名 cUtils.getFiles(dirName, fileName); fileList = cUtils.fileList; //遍历fileList,对每一个文件使用选择的功能 for(String item : fileList) { System.out.println("文件路径为:"+item); if(countChar) { cUtils.countChar(item); } if(countWord) { cUtils.countWord(item); } if(countLine) { cUtils.countLine(item); } } } }
下面是对应的CountUtils.java中的功能函数
-c
//利用BufferedReader整行读取统计字符数 public int countChar(String path) { String flag = "c"; File file = new File(path); BufferedReader br = null; String line; int charNum = 0; try { br = new BufferedReader(new FileReader(file)); while((line = br.readLine()) != null){ char[] ch = line.toCharArray(); for(int i=0; i < ch.length; i++) { if(!Character.isWhitespace(ch[i])) { charNum++; } } } System.out.println("字符数: " + charNum); OutPut(flag, charNum); br.close(); } catch (Exception e) { e.printStackTrace(); System.out.println(path + "文件名错误"); } return charNum; }
-w
public int countWord(String path) { String flag = "w"; BufferedReader br = null; String line; String[] strings; StringBuffer sbf = new StringBuffer(); int wordNum = 0; String reg = "\\s+"; try { br = new BufferedReader(new FileReader(path)); while((line = br.readLine()) != null){ sbf.append(line); } String s = sbf.toString().replaceAll("[^a-zA-Z]", " "); strings = s.split(reg); wordNum = strings.length; System.out.println("单词数: " + wordNum); OutPut(flag, wordNum); br.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } return wordNum; }
-l
//统计总行数 public int countLine(String path) { String flag = "l"; int lineNum = 0; BufferedReader br = null; String line ; try { br = new BufferedReader(new FileReader(path)); while((line = br.readLine())!=null) { lineNum++; } System.out.println("行数:" + lineNum); OutPut(flag, lineNum); br.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } return lineNum; }
将结果输出到指定的TXT文件
//将结果写入TXT文件 public void OutPut(String flag,int num) throws IOException { File f = new File("D:\\Testfile\\OutPut.txt"); if (!f.exists()) { f.createNewFile();//创建新文件 } BufferedWriter output = new BufferedWriter(new FileWriter(f,true));//true,则追加写入text文本 if(flag.equals("c")) { output.write("字符数:"+num); output.write("\r\n"); } if(flag.equals("w")) { output.write("单词数:"+num); output.write("\r\n"); } if(flag.equals("l")) { output.write("行数:"+num); output.write("\r\n"); } output.flush(); output.close(); }
各模块功能测试
本次测试只是用Junit4 对各个功能模块进行了简单的运行测试,保证最基本的正常运行。
将java项目打包成exe
这里推荐这位老师的博客供给参考。讲的十分详细
https://blog.csdn.net/sunkun2013/article/details/13167099
总结
说实话这个项目让我十分难受,一是从来没有很系统的像这样开发程序,觉得费时又很累还没有头绪。收获也确实还是有的,比如 解到gitee、exe4j、单元测试等工具的使用,代码管理也确实很重要,期间因为电脑突然抽风重启导致未Commit的部分又要重新写,又气又无奈。其中对java io流的学习也让我深有收获。
其他资料参考链接
java io流详解
https://www.cnblogs.com/QQ846300233/p/6046388.html
代码参考