Word count

 

1.项目要求

wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:

wc.exe [parameter] [file_name]

基本功能列表:

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

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

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

 

2.遇到的问题

一开始无从毫无头绪,通过百度找到一些相似的作业和一些视频知道了大概的思路,但是java.io流并不会,因此要从头开始学习再实现。

3.代码

 1     import java.io.BufferedReader;
 2     import java.io.File;
 3     import java.io.FileInputStream;
 4     import java.io.FileNotFoundException;
 5     import java.io.IOException;
 6     import java.io.InputStreamReader;
 7     import java.util.Scanner;
 8      
 9     public class Wordcount {
10         public void  Wordcountexe() throws IOException{
11             System.out.println("请输入查找的文件位置:");
12             Scanner sc= new Scanner(System.in); 
13             String filepath=sc.nextLine();
14             BufferedReader br =null;
15             int countWord=0;
16             int countChar=0;
17             int countLine=0;
18             String s="";
19             String strCount="";
20             try {
21                  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filepath))));
22                  while((s=br.readLine())!=null)
23                   {
24                      s=s+" ";
25                      strCount+=s;
26                    countLine++;
27                   }
28                  for(int i=0;i<strCount.split(" ").length;i++)
29                  {
30                      if(!strCount.split(" ")[i].equals(" "))
31                          countWord++;
32                      countChar+= strCount.split(" ")[i].length();
33                  }
34                  System.out.println("单词数:"+countWord);
35                  System.out.println("字符数:"+countChar);
36                  System.out.println("行数:"+countLine);
37                } 
38                    catch (FileNotFoundException e)  
39                    {
40                
41                       e.printStackTrace();
42                    }
43                   finally{
44                             br.close();
45                          }
46         }
47         public static void main(String[] args) throws IOException{
48             Wordcount w=new Wordcount();
49             long d1=System.currentTimeMillis();
50             w. Wordcountexe();
51             long d2=System.currentTimeMillis();
52             System.out.printf("耗时:%sms%n",d2-d1); 
53         }
54     }

3.测试运行

Word countWord count

 

 

Word countWord count

 

 

 Word countWord count

 

 

4.PSP表 

 

  

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

        20

             30

· Estimate

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

       600

     684

Development

开发

         60

     55

· Analysis

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

          240

            376

· Design Spec

· 生成设计文档

          0

             0

· Design Review

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

             0

               0

· Coding Standard

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

             10

              10

· Design

· 具体设计

            40

             33

· Coding

· 具体编码

            40

                40

· Code Review

· 代码复审

            30

               25

· Test

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

            60

            50

Reporting

报告

            25

              20

· Test Report

· 测试报告

             10

              10

· Size Measurement

· 计算工作量

             10

              10

· Postmortem & Process Improvement Plan

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

              20

               25

合计

 

           575

 

        684

 

 

5.小结

这次作业对于有项目经验和已经自学完JAVA的同学来说很简单,但是由于我没有自学JAVA,我并没有轻松的完成作业。甚至只是实现了基本功能而且也没有用工具准确测量代码覆盖率等性能但是从耗时来看实现是十分昂贵的,而且对于GitHub的使用也陌生。应该加快速度学习JAVA,为接下来的作业准备。

GitHub项目地址:https://github.com/Liang9028/work

上一篇:个人项目wc.exe (C++)


下一篇:【刷题】面筋-linux统计文件中字符串次数及文件夹下特定类型文件个数