经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
WordCount统计文档字符数,单词数,行数
来源:cnblogs  作者:Jie140  时间:2018/10/21 20:30:21  对本文有异议

一、项目简介

       源码地址:https://gitee.com/jie140367/WordCount2

  作业地址:https://edu.cnblogs.com/campus/xnsy/Test/homework/2203

       1.项目需求:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。

           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

           wc.exe -s            //递归处理目录下符合条件的文件

  2.开发环境:Idea 2017

  3.开发语言:Java

  4.使用工具:exe4j(将java打包好的jar包转化为exe可执行文件)

    exe4j下载地址:https://www.softpedia.com/get/Authoring-tools/Setup-creators/exe4j.shtml

    exe4j教程:https://blog.csdn.net/xiazdong/article/details/7225734

二、项目实现

  首先放上我的项目目录:

                       

  在拿到这个问题之后,首先要把文档抽象出一个对象,属性包含有字符数,单词数和行数。所以创建了一个java Pojo对象类——Wc.java

  1. package com.jie;
  2. /**
  3. * @Author: jie140
  4. * @Date: 2018/10/20 20:59
  5. */
  6. public class Wc {
  7. //定义字符
  8. public int chars;
  9. //定义单词
  10. public int words;
  11. //定义行数
  12. public int lines;
  13. public int getChars() {
  14. return chars;
  15. }
  16. public int getWords() {
  17. return words;
  18. }
  19. public int getLines() {
  20. return lines;
  21. }
  22. public Wc(int chars, int words, int lines) {
  23. this.chars = chars;
  24. this.words = words;
  25. this.lines = lines;
  26. }
  27. }

之后还有接受参数的主启动类——Main.java

  1. package com.jie;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. public class Main {
  8. public static String inputFile; //输入文件
  9. public static String outputFile; //输出字符文件
  10. public static boolean needC; //判断是否统计字符数 -c
  11. public static boolean needW; //判断是否统计单词数 -w
  12. public static boolean needL; //判断是否统计行数 -l
  13. public static boolean needO; //判断是否输出 -o
  14. public static void main(String[] args)
  15. {
  16. //初始化输入文件
  17. inputFile="D:\\项目\\qnmd.txt";
  18. for(int i=0;i<args.length;i++)
  19. {
  20. System.out.println(args[i]);
  21. if ("-c".equals(args[i])) {
  22. needC = true;
  23. } else if ("-w".equals(args[i])) {
  24. needW = true;
  25. } else if ("-l".equals(args[i])) {
  26. needL = true;
  27. } else if ("-o".equals(args[i])) {
  28. needO = true;
  29. //-o紧挨着输出文件名
  30. outputFile = args[i + 1];
  31. } else {
  32. //如果遇到文件名参数,且前面不是-e和-o,那么这个文件就是输入文件
  33. if (!args[i - 1].equals("-e") && !args[i - 1].equals("-o")) {
  34. inputFile = args[i];
  35. }
  36. }
  37. }
  38. String outputStr="";
  39. //输入文件列表
  40. ArrayList<String> fileNames =new ArrayList<String>();
  41. fileNames.add(inputFile);
  42. //文件数量
  43. int len=fileNames.size();
  44. String fn;
  45. for(int i=0;i<len;i++)
  46. {
  47. fn=fileNames.get(i);
  48. System.out.println(fn);
  49. //分割出实际文件名
  50. String fileShortName=fn.substring(fn.lastIndexOf("\\")+1, fn.length());
  51. if(needC||needW||needL)
  52. {
  53. Wc wc= StatisticalCount.basicInfo(fn);
  54. if(needC)
  55. {
  56. outputStr+=fileShortName;
  57. outputStr+=", char: ";
  58. outputStr+=wc.getChars();
  59. outputStr+="\r\n";
  60. }
  61. if(needW)
  62. {
  63. outputStr+=fileShortName;
  64. outputStr+=", word: ";
  65. outputStr+=wc.getWords();
  66. outputStr+="\r\n";
  67. }
  68. if(needL)
  69. {
  70. outputStr+=fileShortName;
  71. outputStr+=", line: ";
  72. outputStr+=wc.getLines();
  73. outputStr+="\r\n";
  74. }
  75. }
  76. }
  77. System.out.println(outputStr);
  78. if(!needO)
  79. {
  80. //如果没有给输出参数,输出到默认文本中
  81. outputFile="result.txt";
  82. }
  83. try
  84. {
  85. //否则定义新文件,输出到新文件中
  86. File writename = new File( outputFile);
  87. writename.createNewFile();
  88. BufferedWriter out = new BufferedWriter(new FileWriter(writename));
  89. out.write(outputStr);
  90. out.flush();
  91. out.close();
  92. }
  93. catch (IOException e)
  94. {
  95. e.printStackTrace();
  96. }
  97. }
  98. }

其中的逻辑实现类比较复杂,所以又创建了一个专门返回Wc对象的类——StatisticalCount.java,传入空的Wc对象,经过逻辑处理,返回一个具有字符数,单词数和行数的Wc对象。最后打印出来并保存到文件result.txt中。

  1. package com.jie;
  2. import java.io.*;
  3. /**
  4. * @Author: jie140
  5. * @Date: 2018/10/20 20:59
  6. */
  7. public class StatisticalCount {
  8. public static Wc basicInfo(String fileName)
  9. {
  10. //初始化文件的系数值
  11. Wc wc=new Wc(0,0,0);
  12. //当前字符
  13. char charNow;
  14. try
  15. {
  16. //创建文件并读取
  17. File filename = new File(fileName);
  18. InputStreamReader reader = new InputStreamReader(
  19. new FileInputStream(filename));
  20. BufferedReader br = new BufferedReader(reader);
  21. //读取一行的数据
  22. String line ;
  23. line = br.readLine();
  24. //是否分割
  25. boolean partition=true;
  26. while (line != null)
  27. {
  28. //获取一行中字符长度
  29. wc.chars+=line.length();
  30. wc.lines++;
  31. for(int i=0;i<line.length();i++)
  32. {
  33. //当前字符
  34. charNow=line.charAt(i);
  35. //分割出单词
  36. if(partition==true&&charNow!=' '&&charNow!='\t'&&charNow!=','&&charNow!=',')
  37. {
  38. wc.words++;
  39. }
  40. //分割
  41. if(charNow==' '||charNow=='\t'||charNow==','||charNow==',')
  42. {
  43. partition=true;
  44. }
  45. }
  46. line = br.readLine();
  47. }
  48. //加上回车长度
  49. wc.chars+=wc.lines-1;
  50. br.close();
  51. }
  52. catch (IOException e)
  53. {
  54. e.printStackTrace();
  55. }
  56. return wc;
  57. }
  58. }

三、测试项目

    1、等价类划分:  

输入 有效等价类 无效等价类
wc.exe -参数 文件 参数-c 除了-c,-w,-l,-o之外的参数输入,错误的文件名
参数-l
参数-w
参数-o
正确存在的文件名

    2、测试:

        

输入 结果
wc.exe -c 作业.txt, char: 37
wc.exe -l 作业.txt, line: 4
wc.exe -w 作业.txt, word: 31
wc.exe -c -w

作业.txt, char: 37
作业.txt, word: 31

wc.exe -c -w -o result2.txt

作业.txt, char: 37
作业.txt, word: 31

wc.exe -z

文件无输出

    3.测试文件内容

 

                            

    4.执行结果

                            

四、项目总结

该项目主要运用到的思想是 面向对象的思想。

1:通过外部dos命令传入到Main函数中args数组中,通过循环和条件语句进行分功能运行。

2.使用了exe4j之后会弹窗,不能输出控制台语句。

3.调用-c -w -l 的功能中间使用到了大量的IO知识和文件操作,不了解的小伙伴请多参考几篇博客进行知识补充。

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号