1,项目地址:
https://github.com/Linqinf/Personal
2.PSP
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
30 |
60 |
· Estimate |
· 估计这个任务需要多少时间 |
15 |
18 |
Development |
开发 |
|
|
· Analysis |
· 需求分析 (包括学习新技术) |
2*60 |
3*60 |
· Design Spec |
· 生成设计文档 |
10 |
15 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
0 |
0 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
5 |
5 |
· Design |
· 具体设计 |
60 |
120 |
· Coding |
· 具体编码 |
60*8 |
60*10 |
· Code Review |
· 代码复审 |
120 |
240 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
60 |
120 |
Reporting |
报告 |
60 |
60 |
· Test Report |
· 测试报告 |
30 |
30 |
· Size Measurement |
· 计算工作量 |
20 |
15 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
15 |
20 |
合计 |
|
1025 |
1483 |
3.解题思路
由于刚刚新学了JAVA入门知识,想借此机会巩固一下,考虑程序的设计需求,又去新学了FIle类 和 IO的一些知识,基本思路就是利用java的BufferedReader 字符输入流逐行读取文件信息。
4.设计思路
整个项目工程包主要包括了3个类,其中一个是主类,一个是统计文件信息的类,还有一个是画GUI的类。最后导出jar包,用exe4j软件,生成一个能在含有jre上的系统运行的exe程序。
5.主要代码
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Recursion { static int line = 0; static int words = 0; static int character = 0; static int blank = 0; static int comment = 0; static int codeline = 0; static int FilesNumber; static String msg = ""; public String showFile(File file){ calculation(file); return msg; } public String showFile(File file ,String order , String format){//D://test文件夹 File[] files = file.listFiles();//test文件夹所有子元素 if(files!=null && files.length!=0){ for(File f:files){//遍历每个子文件 this.showFile(f,order,format);//递归子元素 } } if(file.getAbsolutePath().split("\\.").length == 2){ if(file.isDirectory() == false && (file.getAbsolutePath().split("\\.")[1].equals(format)) || format.equals("*")){ //判断文件类型和文件格式 FilesNumber++; msg += "第" + FilesNumber + "个文件名为" ; System.out.println("第" + FilesNumber + "个文件为" + file.getAbsolutePath()); calculation(file);//进行统计计算 if(order.equals("-c")){ System.out.println("文本的字符数为"+Recursion.character); } else if(order.equals("-w")){ System.out.println("文本的字符数为"+Recursion.words); } else if(order.equals("-l")){ System.out.println("文本的行数为"+Recursion.line); } else if(order.equals("-a")){ System.out.println("文本的空白行为"+Recursion.blank); System.out.println("文本的注释行为"+Recursion.comment); System.out.println("文本的代码行为"+Recursion.codeline); } else return msg; } } return msg; } public String calculation(File file){ line = 0; words = 0; character = 0; blank = 0; comment = 0; codeline = 0; msg += file.getAbsolutePath() + "\n"; BufferedReader bf = null; FileReader fr = null; try { fr = new FileReader(file);//读文件 bf = new BufferedReader(fr); String value = bf.readLine(); while(value != null){ value = value.trim();//去除前后的空格 if(value.equals("")||value.matches("\\{|\\}")) blank++; //累加空白行数 else if (value.matches("(//|\\{//|\\}//).*")) comment++; //累加注释行数 else codeline++; //代码行数 //msg += value + "\n"; String r = value.replaceAll("\\.",""); r = r.replaceAll("[^\\w]", " ");//特殊符号都去除,变为空格,方便统计单词数 r = r.replaceAll("\\s+"," ").replaceAll("\""," "); r = r.trim();//去除头尾的空格 String s[] = r.split(" ");//按空格拆分得到单词 if(!s[0].equals("")) {//空行不计算单词数 words += s.length; //累加单词数 } character += value.length();//累加字符数量 line++;//累加行数 value = bf.readLine();//读取下一行,重新进行循环 } msg = msg + "文本行数为 " + line + "\n" + "文本的单词数量为 " + words + "\n" + "文本的字符数为 " + character + "\n" + "文本的空白行为 " + blank + "\n" + "文本的注释行为 " + comment + "\n" + "文本的代码行为 " + codeline + "\n" + "\n"; } catch (IOException e) { //编译时异常 e.printStackTrace(); }finally { try { fr.close(); bf.close(); } catch (IOException e) { e.printStackTrace(); } } return msg; } }
6.测试运行
7.项目小结
第一次动手用JAVA写项目,觉得难度还是蛮大的,在这过程中又学习了例如File类,IO流,正则表达式等新知识,在动手敲代码时候,整个工程的结构又一直在改,有点乱,应该提前规划好
整个项目的结构,还有平时学习新知识时候应多做笔记,免得真正用的时候又全忘了。