java实现WordCount

一、Gitee地址:

https://gitee.com/dzl-git/WordCount

二、解题思路:

1、该程序要求在控制台输入参数后运行,所以编程时将输入的参数存入args[]中进行逐个判断。

2、作业要求对指定文本内的字符数、单词总数和行数进行统计,且可指定输出至文件,以及一些其它扩展内容。

3、将文本内容读取进String中,对该字符串进行检测则可得出结果。

4、统计单词个数时,与停用词表中的单词逐个比较,若相同则不计数。

5、注释行、空行、代码行的判断通过正则表达式判断。

三、程序的设计实现过程:

public static String getC(String fileName)    // 计算指定文件中字符的个数

public static String getW(String fileName)   // 计算指定文件中单词的个数

public static String getl(String fileName)     //计算指定文件中的总的行数

public static String geta(String fileName)    //返回具体的代码行数, 注释行数和空的行数

public static void writeFile(String fileName)   // 将结果写入指定的文件中

四、代码说明:

// 计算指定文件中字符的个数,
    public static String getC(String fileName) {
        int count = 0;
        String s;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            // FileReader in = new FileReader(fileName);
            while ((s = in.readLine()) != null)
                count += s.length();
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName + ", 字符数为:" + count;
    }
  // 计算指定文件中单词的个数
    public static String getW(String fileName) {
        int count = 0;
        String s;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while ((s = in.readLine()) != null) {
                if (!s.isEmpty()) {
                    String ss[] = s.trim().split(" |,|    ");
                    count += ss.length;
                }
            }
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName + ", 单词数为:" + count;
    }
//计算指定文件中的总的行数
    public static String getl(String fileName) {
        int count = 0;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while ((in.readLine()) != null)
                ++count;
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName + ", 总的行数为:" + count;
    }
//返回具体的代码行数, 注释行数和空的行数
    public static String geta(String fileName) {
        int count = 0, space = 0, h = 0;
        String s;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while ((s = in.readLine()) != null) {
                if (s.isEmpty() || s.trim().length() <= 1)
                    ++space;
                else {
                    int t = 0;
                    while (t < s.length() && s.charAt(t) == ' ')
                        ++t;
                    if ((t + 1 < s.length() && s.charAt(t) == '/' && s.charAt(t + 1) == '/')
                            || (t + 2 < s.length() && s.charAt(t + 1) == '/' && s.charAt(t + 2) == '/'))
                        ++h;
                    else
                        ++count;
                }
            }
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName + ", 代码行/空行/注释行:" + count + "/" + space + "/" + h;
    }
// 将结果写入指定的文件中
    public static void writeFile(String fileName) {
        try {
            FileWriter w = new FileWriter(fileName);
            int i = 0;
            for (; i < arrayList.size(); i++) {
                w.write(arrayList.get(i));
                w.write("\r\n");
            }
            w.close();
        } catch (IOException e) {
            System.out.println("文件写入错误");
            e.printStackTrace();
        }
    }
//主函数
public static void main(String[] args) {
        if (args.length > 1) {
            int last = args.length - 1;
            String name = args[last]; 
            String outputName = ""; 
            String tmp;
            int flag = 0;
// 判断是否需要将信息输出到一个文件中 for (int i = 0; i < last; i++) if (args[i].equals("-o")) { flag = 1; name = args[last - 2]; outputName = args[last]; break; } // 读取命令行中传入的参数 for (int i = 0; i < last; i++) if (args[i].equals("-c")) { tmp = new String(getC(name)); System.out.println(tmp); arrayList.add(tmp); } else if (args[i].equals("-w")) { tmp = new String(getW(name)); System.out.println(tmp); arrayList.add(tmp); } else if (args[i].equals("-l")) { tmp = new String(getl(name)); System.out.println(tmp); arrayList.add(tmp); } else if (args[i].equals("-a")) { tmp = new String(geta(name)); System.out.println(tmp); arrayList.add(tmp); } // 将结果输出到一个文件中 if (flag == 1) { writeFile(outputName); System.out.println(); System.out.println("将信息写入" + outputName + "文件成功!!!"); } } else System.out.println("请先输入参数!!!"); }

五、测试用例设计:

测试字符个数::   wc.exe -c test.txt -o result.txt

 

测试单词个数:   wc.exe -w test.txt -o result.txt

 

测试总行数:   wc.exe -l test.txt -o result.txt

 

测试代码行数, 注释行数和空的行数:   wc.exe -a test.txt -o result.txt

 

所有功能测试:  wc.exe -c -w -l -a test.txt -o result.txt

 

六、总结:

在编码实现的过程中遇到了很多问题,好在网上有很多参考代码,然后自己根据参考代码写了出来。通过这次作业学会了Git的使用。

参考文献链接

http://www.cnblogs.com/xinz/p/5044037.html  《现代软件工程讲义 源代码管理》邹欣

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000  Git教程 - 廖雪峰的官方网站

http://blog.csdn.net/sunkun2013/article/details/13167099  《手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件》孙琨

 

posted on 2018-09-24 15:21  ghost9526  阅读(1654)  评论(1编辑  收藏  举报