这里是该项目作业的要求:
https://edu.cnblogs.com/campus/xnsy/2018Systemanalysisanddesign/homework/2120 ---陈汶滨老师
github:
https://github.com/DngLing/CountWord
一、语言选择
我选择了java来完成我的项目作业,我选择的理由是:
1.最近真正学习java的常用的类以及IO流;
2.项目中很多功能的实现都可以使用java中一些已有的类(例如在解析大段文字时,Stirng类提供了很多实用方法);
3.我没有打算做界面系统,不然我还是倾向于学过的C#
二、需求分析与可行性
需求分析
1.通过文件地址获得该文件的内容(这个文件是一个文本文件,保存着代码);
2.得到目标文件中的字符数、单词数、行数、代码行、空行、注释行。且对这六种中一些数据做了解释:
1)字符:所有的空格也算字符;
2)单词:由空格或逗号隔开的单词,并且不做有效性判断;
3)代码行:非空行注释行;
4)注释行 以“ // ”,“ /* ”,“ * ”开头的行,或者以“ */ ”,“ // ”结尾的行,如果一行代码后跟了注释,那么改行也算代码行。
5)空行:改行没有代码,全部是空格或者控制符,如果有,不能超过一个可显示字符,例如“ { ”。
3.将2中的得到的信息写出到一个新的文本文件,且 该文件与传入的文件在同一文件夹下;
4.非图形界面需要输入一些指令来执行操作
可行性分析
1.通过IO流可以轻松实现文件的读写
三、具体设计与编码
这是我在ProcessON上制作的类设计图
IO_uitl.class
这个类实现了IO流的输入与输出,他有两个方法,ReadLine()和WriteLine()
public ArrayList<String> ReadLine(String srcPath) throws IOException{ ArrayList<String> strs = new ArrayList<String>(); File srcFile = new File(srcPath); BufferedReader bReader = null; try { bReader = new BufferedReader(new FileReader(srcFile)); String line; while(null!=(line =bReader.readLine())){ strs.add(line); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ bReader.close(); } return strs; }
Parser.class 该类将对字串进行解析,得到所需要的结果
下面是部分代码:
public int getCommtLineNum(ArrayList<String> str){ int num =0; for(int i=0;i<str.size();i++){ String temp = str.get(i).trim(); if(temp.startsWith("//")||temp.startsWith("/*")||temp.startsWith("*")||temp.endsWith("*/")){ num++; } } return num; } public boolean isCommtLine(String str){ boolean flag =false; String temp = str.trim(); if(temp.startsWith("//")||temp.startsWith("/*")||temp.startsWith("*")||temp.endsWith("*/")||temp.startsWith("}//")){ flag = true; } return flag; } public boolean isEmptyLine(String str){ boolean flag = false; if(this.getWordsNum(str)==0&&!this.isCommtLine(str)){ flag = true; } return flag; }
FileInfo.class 是一个JavaBean,用于传递一个文件的信息。
其它的代码我已经放在了github上,这里由于篇幅原因不在列出。
--------------------------------------------------------------------------------
今天先写到这儿,明儿个再说