用JAVA实现WordCount程序

github项目地址:https://github.com/BiuBiuBangBoom/wc

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

  30    40

· Estimate

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

扫描二维码关注公众号,回复: 3200187 查看本文章
 180  240

Development

开发

 150  180

· Analysis

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

 15  15

· Design Spec

· 生成设计文档

 20  20

· Design Review

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

 20  25

· Coding Standard

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

 10  15

· Design

· 具体设计

 18  15

· Coding

· 具体编码

 120  150

· Code Review

· 代码复审

 30  35

· Test

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

 10  10

Reporting

报告

 15  20

· Test Report

· 测试报告

 15  15

· Size Measurement

· 计算工作量

 12  30

· Postmortem & Process Improvement Plan

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

 15  15

合计

   635  825

一.项目简介

能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有 wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。

二.完成情况

基本功能:

  • [x] -c <文件名> ,统计文件字符数。
  • [x] -w <文件名> ,统计文件单词数。
  • [x] -l <文件名> ,统计文件行数数。

扩展功能

  • [x] -s <文件路径+类型(?任意)> ,递归符合条件的文件。
  • [x] -a <文件名> ,统计文件代码行,空白行,注释行。

说明:以上命令可叠加使用,即 -c -w -l <文件名> ,统计文件的字符数,单词数和行数。

三.解题思路

1.利用正则表达式校验输入参数,然后解析出相关参数和文件地址。

2.利用正则表达式和BufferReader的readline()方法进行各种计数

四.设计流程

项目中含有一个包,里面里有一个实现功能的主类,及一个测试用的类。其中主类中包含主函数,递归处理文件函数,行计数函数,词计数函数,字符计数函数,拓展功能实现函数。

五.代码说明

递归收集文件夹下符合要求的文件

    public void getFolders(String filepath) {
        File file = new File(filepath);
        File[] folders = file.listFiles();
        if (folders.length == 0)
            return;
        else
            for (File f : folders) {
                if (f.isDirectory())
                    getFolders(f.getAbsolutePath());//递归文件夹
                else if (f.getName().endsWith("java"))
                    files.add(f);
            }
    }

计算词数

    public void wordsCount(String filepath) throws IOException {
        int count = 0;
        File file = new File(filepath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        Pattern pa = Pattern.compile("[\u4e00-\u9fa5]");
        while((line = br.readLine()) != null) {
            String[] str = line.split("\\W+");
            for(String word : str) {
                if(!word.equals("") && !pa.matcher(word).find())//检查是否有中文或空字符
                        count++;
            }
        }
        System.out.println("词数:" + count);
        br.close();
        return;
    }

计算字符数

    public void charCount(String filepath) throws IOException {
        int count = 0;
        File file = new File(filepath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line1, line2;
        while((line1 = br.readLine()) != null) {
            line2 = line1.replaceAll("\\s+", "");//将空白符删去
            count += line2.length();
        }
        System.out.println("字符数:" + count);
        br.close();
        return;
    }

计算行数

public void linesCount(String filepath) throws IOException {
        int count = 0;
        File file = new File(filepath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        while(br.readLine() != null)
            count++;
        System.out.println("行数:" + count);
        br.close();
        return;
    }

拓展功能实现,计算空白行,注释行,代码行

public void advancedCount(String filepath) throws IOException {
        int emptyLineCount = 0;
        int codeLineCount = 0;
        int comLineCount = 0;
        File file = new File(filepath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine()) != null) {
            if(Pattern.compile("^\\s*\\S?\\s*$").matcher(line).matches())//检验空行
                emptyLineCount++;
            else if(line.contains("//"))
                comLineCount++;
            else
                codeLineCount++;
        }
        System.out.println("空行:" + emptyLineCount);
        System.out.println("代码行:" + codeLineCount);
        System.out.println("注释行:" + comLineCount);
        br.close();
        return;
    }

六.测试

程序启动

测试空文件

只有一个字符的文件

 递归文件调用

七.项目总结

通过本次项目,我对于从前比较模糊的文件输入跟正则表达式有了较好的掌握,同时也对字符串类型方法有了更深的了解。在开发过程中,我经常遇到不知道如何实现某个功能,但通过查看文档往往会发现有很多很好用的方法,使我对功能的实现有了灵感。就此我深感文档对于开发的重要性。

猜你喜欢

转载自www.cnblogs.com/6324tv-upup/p/9649740.html
今日推荐