第一次做业——WorkCount

项目地址:https://gitee.com/yangfj/wordcount_projecthtml

1.软件需求分析:java

撰写PSP表格:git

PSP2.1数组

PSP阶段安全

预估耗时函数

(分钟)工具

实际耗时单元测试

(分钟)学习

Planning测试

计划

 20

 15

· Estimate

· 估计这个任务须要多少时间

20

 15

Development

开发

 960

 880

· Analysis

· 需求分析 (包括学习新技术)

 10

 20

· Design Spec

· 生成设计文档

 40

 20

· Design Review

· 设计复审 (和同事审核设计文档)

 10

 30

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

50

 10

· Design

· 具体设计

 50

 20

· Coding

· 具体编码

 500

 650

· Code Review

· 代码复审

 100

 30

· Test

· 测试(自我测试,修改代码,提交修改)

 200

 50

Reporting

报告

150

 100

· Test Report

· 测试报告

 80

 50

· Size Measurement

· 计算工做量

 20

 20

· Postmortem & Process Improvement Plan

· 过后总结, 并提出过程改进计划

 50

 30

 

合计

 1130

 995

 

结果分析,在完成编码撰写博客的时候,确实发现了许多计划与实际出入的地方,好比说代码规范,在实际编码的时候,根本没有考虑到这一点,只是在最终编写完成以后才意识到,因而最后才开始改编码规范,写次开发的时候就应该提早考虑,虽然只是一我的在开发。还有就是整体来讲并非很完整的进行了测试,只是有一些单元测试,之后会多了解一下代码测试这一块的技能。

2.解题思路

由于之前用到的代码管理工具都是SVN,尚未用过Git ,只是偶尔在GitHub上面逛逛,因此首先想的是成功安装Git,而且实现代码管理,而后就找了很久的资料,包括Git如何提交到码云等等,详见个人另外一篇博文:

https://www.cnblogs.com/shoneworn/p/8251556.html

而后看了看题目,发现应该先肯定用什么语言代码来实现,虽然本身擅长C#,也学过Python,但仍是决定用java语言比较好,一个是由于本身如今正在学习java,正好能够经过这个项目提高本身,主要采用的技术是java IO流方面的技术。

1.对于常见的java项目,应该利用什么才能实如今命令行中进行输入输出的信息读取?

2.如何判断控制台是要求的统计字符数,单词数,总行数,存入特定文件的相应基础功能?

3.如何在支持基础功能的基础上,拓展其待实现的功能而又能与原代码区分开来?

 3.需求分析

具体的需求其实大体能够总结为一下几点:

1.输入:

输入指定的文件,采用程序统计文件的字符数、单词数、行数、

2.输出:

输出文件(以指定的格式)

3.拓展:

支持 返回高级选项(代码行 空行 注释行)

递归处理符合条件的文件,考虑程序的时间复杂度和空间复杂度

4.程序实现过程

项目一共两个文件,一个为主函数,一个为实现具体功能类。

mian函数:

public class WorkCount { public static void main(String[] args) throws Exception { // 统计一个文件的字符数,单词数,行数
          Scanner input = new Scanner(System.in); System.out.println("please input path:"); String path = input.next(); int countChar = 0; int countword = 0; int countline = 0; InputStreamReader isr = new InputStreamReader(new FileInputStream(path)); //InputStreamReader将字符流向字节流转换。 //InputStreamReader isr = new InputStreamReader(new FileInputStream(绝对文件名)); //用来读取文件中的数据
          BufferedReader br = new BufferedReader(isr);//使用缓冲区,可使用缓冲区的read(),readLine()方法;
          while(br.read()!=-1)//read()=-1表明数据读取完毕
 { String s = br.readLine(); countChar += s.length();//字符个数就是字符长度
           countword += s.split(" ").length;//split() 方法用于把一个字符串分割成字符串数组,字符串数组的长度,就是单词个数
           countline++;//由于是按行读取,因此每次增长一便可计算出行的数目
 } isr.close();//关闭文件
          System.out.println("char cont "+countChar); System.out.println("word count "+countword ); System.out.println("line count "+countline); } }

实现具体功能类:

返回字符的个数

int  CharCount(String s)//用来输入文件并返回字符的个数 
{ int count = 0; Pattern p = Pattern.compile("[a-zA-Z]"); Matcher m = p.matcher(str); while(m.find()){ count++; } return count; } 
}

返回单词出现总数 :

public static int countdanci(String[] args) { //逐行读文件
 BufferedReaderbr = null; try { Map<String,Integer>map = newHashMap<String,Integer>(); br = new BufferedReader(new FileReader("d:/mywords.txt")); Stringline; while(null != (line = br.readLine())){ System.out.println(line); //将字符串用空格分隔
 String[]ss = line.split("\\s+"); for(String s : ss){ if(map.containsKey(s)){ map.put(s, map.get(s)+1); }else{ map.put(s, 1); } } } Set<String>keys = map.keySet(); for(String key : keys){ System.out.println(key + "有:" + map.get(key) + "个."); } }catch(FileNotFoundException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); }finally { if(null != br){ try { br.close(); }catch(IOException e) { e.printStackTrace(); } } }

文件总行数:

public static HangCount(String[] args) { try{ File file =new File("c:\\test.txt"); if(file.exists()){ FileReader fr = new FileReader(file); LineNumberReader lnr = new LineNumberReader(fr); int linenumber = 0; while (lnr.readLine() != null){ linenumber++; } System.out.println("Total number of lines : " + linenumber); lnr.close(); }else{ System.out.println("File does not exists!"); } }catch(IOException e){ e.printStackTrace(); } }

文件空白行数,注释行数

public static void CountLine(File f) throws FileNotFoundException, IOException{ //经过调用一个以前定义的对象来表示此文件链接,file参数表示的路径做为参数调用javaLine方法
        String strLine = "";//建立了一个对象。而且加入字符串池中
 String str = fromFile(f); //str为引用 "" 是 引用指向的的值,定义一个String 类型的变量str,并为其赋值
        if (str.length() > 0) { while (str.indexOf('\n') != -1) { //经过一个wile循环体判断这个值,从而作到字符串处理,使得字符串以正确方式显示
 totle++; strLine = str.substring(0, str.indexOf('\n')).trim(); if (strLine.length() == 0 ) { blank++; }else if (strLine.charAt(0) == '*' || strLine.charAt(0) == '/') { comments++; }else{ source++; String regEx = "^*//"; if(regEx(strLine,regEx)){ comments++; } } str = str.substring(str.indexOf('\n') + 1, str.length()); //返回给定区间的字符串,以上通过转换后,str可以正常显示
 } } }

输出到文件:

public void outPutfile(String infilename,String outfiename) { try { fileReader(infilename); File file=new File(outfiename); if(!file.exists()) { file.createNewFile(); } String lineSents=""; String wordSents=" "; String charSents=" "; String charCon=CharCount/callCounter+""; String lineCon=LineCount/callCounter+""; String wordCon=WordCount/callCounter+""; charSents=charSents.concat(charCon).concat("\r\n");//使用concat()方法连接两个字符串
            lineSents=lineSents.concat(lineCon).concat("\r\n"); wordSents=wordSents.concat(wordCon).concat("\r\n"); FileWriter fw=new FileWriter(file.getAbsoluteFile()); BufferedWriter bWriter=new BufferedWriter(fw); bWriter.write(charSents);//写入到文件
 bWriter.write(lineSents); bWriter.write(wordSents); bWriter.close();//必须关闭才能写入文件,不然写入无效
 fw.close(); if(file.length()!=0) System.out.println("write file succeed..."); else System.out.println("write file failed..."); } catch (IOException e) {//跑出异常 // TODO Auto-generated catch block
 e.printStackTrace(); } }

5.测试过程:

wc.exe -c file.c     //返回文件 file.c 的字符数

wc.exe -w file.c     //返回文件 file.c 的单词总数

wc.exe -l file.c     //返回文件 file.c 的总行数

wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

......

6.测试结果:

wc.exe -c C:\Learning\Java\code\temp\src\com\hcedu\dao\LoginDao.java

wc.exe -w C:\Learning\Java\code\temp\src\com\hcedu\dao\LoginDao.java

 

wc.exe -l C:\Learning\Java\code\temp\src\com\hcedu\dao\LoginDao.java

 

wc.exe -c C:\Learning\Java\code\temp\src\com\hcedu\dao\LoginDao.java

wc.exe -m C:\Learning\Java\code\temp\src\com\hcedu\dao\LoginDao.java

7.总结

 此次的WorkCount做业,实际上是一个较为完整的项目,其中包括了需求分析、系统设计、编码实现、测试分析等等。之前写代码并无写过如此完整的代码流程,其中不少时候都没有写过文档和测试分析,经过此次的编码,我意识到了代码的安全性,程序的健壮性。规范的代码更对以后的测试分析有利。同时更要考虑到用户的感觉,让代码变的更加人性化才行。

最后,其实此次的代码我还存在着许多的不足,我最开始拿到这个项目,写完需求分析以后就没有动过博客,后面逐一实现代码以后又对博客进行的删减去,总的来讲,仍是一直都在写代码,而忽视了文档的重要性,在之后的编码过程当中,更应该增强对文档的投入。

8.参考文献

邹欣老师在《构建之法》中设计的第一项我的做业:http://www.cnblogs.com/xinz/p/7426280.html

java统计文件中字符,数字,汉字,空格数目: https://blog.csdn.net/lalaxumelala/article/details/79532159
【Java】统计文件中各单词数量:https://blog.csdn.net/cassie_m/article/details/77512473
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息