一.Gitee地址:https://gitee.com/zjgss99/WordCount

二.项目分析:

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

命令格式:

wc.exe [para] <filename> [para] <filename> ... -o <filename>

基础功能:

-c:统计文件中的字符数,不包括换行符;

-w:统计文件中的单词数;

-l:统计文件的行数;

-o:指定输出文件;

三.PSP表格:

PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 15 10
· Estimate · 估计这个任务需要多少时间 15 10
Development 开发 400 660
· Analysis · 需求分析(包括学习新技术) 30 90
· Design Spec · 生成设计文档 30 20
· Design Review · 设计复审(和同事审核设计文档) 10 20
· Coding Standard · 代码规范(为目前的开发制定合适的规范) 5 5
· Design · 具体设计 30 15
· Coding · 具体编码 240 450
· Code Review · 代码复审 40 30
· Test · 测试(自我测试,修改代码,提交修改) 15 30
Reporting 报告 65 35
· Test Report · 测试报告 20 10
· Size Measurement · 计算工作量 15 10
· Postmortem & Process improvement Plan · 事后总结,并提出过程改进计划 30 15 
  合计 480

705

四.解题思路:

项目大致分为三个部分:

1)对用户输入的命令进行判断,读取文件,处理,传递参数给功能处理部分

2)对几种命令对应的功能分别进行实现,接收参数

3)根据命令将输出结果保存到相应的输出文件中

项目构成:

1)主函数:

读取文件,对用户输入的命令分别调用功能处理模块,并对一些异常情况做处理

2)功能处理模块:

对基本功能进行实现,通过主函数传递的参数确定需要输出的输出文件及输出文件需要的内容。

五.代码展示:

1)模块处理方法(通过主方法传递的参数确定输出内容)

  1. import java.io.*;
  2.  
  3. public class Handle {
  4.  
  5. int line = 0;
  6. int word = 0;
  7. int charnum = 0;
  8.  
  9. public void deal(String readPath,String writePath, String flag) {
  10. boolean flagexist = true;
  11. try {
  12. String str = "";
  13. String[] linenum;
  14. File file = new File(readPath);
  15. BufferedReader br = new BufferedReader(new FileReader(readPath));
  16. try {
  17. try {
  18. while ((str = br.readLine()) != null) {
  19. linenum = str.split(",| ");
  20. for (int i = 0; i < linenum.length; i++) {
  21. if (linenum[i] != null)
  22. word++;
  23. }
  24. line++;
  25. charnum += str.length();
  26. }
  27. System.out.println("行数:"+line+ " 单次数:" + word+" 字符数:"+ charnum);
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. }
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } finally {
  34. try {
  35. if (br != null) {
  36. br.close();
  37. }
  38. } catch (IOException e) {
  39. System.out.println("关闭BufferedReader错误");
  40. }
  41. }
  42. } catch (FileNotFoundException e) {
  43. flagexist = false;
  44. System.out.println("未找到文件.");
  45. }
  46. if(!flagexist){
  47.  
  48. }
  49. else{
  50. String output = "";
  51. switch (flag){
  52. case "-o":
  53. output = readPath+",字符数:"+charnum;
  54. try{
  55. File outputFile = new File(writePath);
  56. outputFile.createNewFile();
  57. BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
  58. bw.write(output);
  59. bw.flush();
  60. bw.close();
  61. }catch (IOException e)
  62. {
  63. e.printStackTrace();
  64. }
  65. break;
  66. case "-w":
  67. output = readPath+",单词数:"+word;
  68. try{
  69. File outputFile = new File(writePath);
  70. outputFile.createNewFile();
  71. BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
  72. bw.write(output);
  73. bw.flush();
  74. bw.close();
  75. }catch (IOException e)
  76. {
  77. e.printStackTrace();
  78. }
  79. break;
  80. case "-l":
  81. output = readPath+",行数:"+line;
  82. try{
  83. File outputFile = new File(writePath);
  84. outputFile.createNewFile();
  85. BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
  86. bw.write(output);
  87. bw.flush();
  88. bw.close();
  89. }catch (IOException e)
  90. {
  91. e.printStackTrace();
  92. }
  93. break;
  94. case "-c":
  95. output = readPath+",字符数:"+charnum;
  96. try{
  97. File outputFile = new File(writePath);
  98. outputFile.createNewFile();
  99. BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
  100. bw.write(output);
  101. bw.flush();
  102. bw.close();
  103. }catch (IOException e)
  104. {
  105. e.printStackTrace();
  106. }
  107. break;
  108. }
  109. }
  110. }
  111. }

2)主方法(对用户的输入命令进行处理并传递给功能处理模块)

  1. import java.io.*;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) throws IOException {
  6. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  7. String read = null;
  8. System.out.println("请输入命令(格式:wc.exe [parameter] [input_file_name]):");
  9. try{
  10. String readPath = "file.c";
  11. String writePath = "result.txt";
  12. read = bf.readLine();
  13. // System.out.println(read);
  14. String [] getRead;
  15. getRead = read.split(" ");
  16. if(getRead.length == 3){
  17. if(getRead[0].equals("wc.exe")){
  18. if(getRead[1].equals("-o")){
  19. if(getRead[2].endsWith(".txt")){
  20. Handle handle = new Handle();
  21. writePath = getRead[2];
  22. handle.deal(readPath,writePath,"-o");
  23. }
  24. else {
  25. System.out.println("命令格式输入错误");
  26. }
  27. }
  28. else if(getRead[1].equals("-c")){
  29. if(getRead[2].endsWith(".c")){
  30. Handle handle = new Handle();
  31. readPath = getRead[2];
  32. handle.deal(readPath,writePath,"-c");
  33. }
  34. else {
  35. System.out.println("命令格式输入错误");
  36. }
  37. }
  38. else if(getRead[1].equals("-w")){
  39. if(getRead[2].endsWith(".c")){
  40. Handle handle = new Handle();
  41. readPath = getRead[2];
  42. handle.deal(readPath,writePath,"-w");
  43. }
  44. else {
  45. System.out.println("命令格式输入错误");
  46. }
  47. }
  48. else if(getRead[1].equals("-l")){
  49. if(getRead[2].endsWith(".c")){
  50. Handle handle = new Handle();
  51. readPath = getRead[2];
  52. handle.deal(readPath,writePath,"-l");
  53. }
  54. else {
  55. System.out.println("命令格式输入错误");
  56. }
  57. }
  58. else {
  59. System.out.println("命令格式输入错误");
  60. }
  61. }
  62. else{
  63. System.out.println("可执行文件名输入错误");
  64. }
  65. }
  66. else{
  67. System.out.println("命令输入格式错误.");
  68. }
  69. }catch(Exception e){
  70. e.printStackTrace();
  71. }
  72. }
  73. }

六.测试

正常按格式输入命令

错误的输入

七.参考文献:

java文件操作 https://www.cnblogs.com/xwlych/p/5987022.html

将jar包生成.exe文件 https://blog.csdn.net/u011752272/article/details/80697198

WordCount的更多相关文章

  1. hadoop 2.7.3本地环境运行官方wordcount

    hadoop 2.7.3本地环境运行官方wordcount 基本环境: 系统:win7 虚机环境:virtualBox 虚机:centos 7 hadoop版本:2.7.3 本次先以独立模式(本地模式 ...

  2. Hadoop3 在eclipse中访问hadoop并运行WordCount实例

    前言:       毕业两年了,之前的工作一直没有接触过大数据的东西,对hadoop等比较陌生,所以最近开始学习了.对于我这样第一次学的人,过程还是充满了很多疑惑和不解的,不过我采取的策略是还是先让环 ...

  3. Eclipse 执行成功的 Hadoop-1.2.1 WordCount 源码

    万事开头难.最近在学习Hadoop,先是搭建各种版本环境,从2.2.0到2.3.0,再到1.2.1,终于都搭起来了,折腾了1周时间,之后开始尝试使用Eclipse编写小demo.仅复制一个现成的Wor ...

  4. 软件工程:Wordcount程序作业

    由于时间的关系,急着交作业,加上这一次也不是那么很认真的去做,草草写了“Wordcount程序”几个功能,即是 .txt文件的读取,能计算出文件内容的单词数,文件内容的字符数,及行数. 这次选用C来做 ...

  5. Spark源码编译并在YARN上运行WordCount实例

    在学习一门新语言时,想必我们都是"Hello World"程序开始,类似地,分布式计算框架的一个典型实例就是WordCount程序,接触过Hadoop的人肯定都知道用MapRedu ...

  6. MapReduce剖析笔记之一:从WordCount理解MapReduce的几个阶段

    WordCount是一个入门的MapReduce程序(从src\examples\org\apache\hadoop\examples粘贴过来的): package org.apache.hadoop ...

  7. 软件工程-构建之法 WordCount小程序 统计文件中字符串个数,单词个数,词频,行数

    一.前言 在之前写过一个词频统计的C语言课设,别人说你一个大三的怎么写C语言课程,我只想说我是先学习VB,VB是我编程语言的开始,然后接触到C语言及C++:再后来我是学习C++,然后反过来学习C语言, ...

  8. eclipse连hadoop2.x运行wordcount 转载

    转载地址:http://my.oschina.net/cjun/blog/475576 一.新建java工程,并且导入hadoop相关jar包 此处可以直接创建mapreduce项目就可以,不用下面折 ...

  9. Hadoop中wordcount程序

    一.测试过程中 输入命令: 首先需要在hadoop集群中添加文件 可以首先进行查看hadoop集群中文件目录 hadoop fs -ls / hadoop fs -ls -R / hadoop fs ...

  10. Hadoop示例程序WordCount详解及实例(转)

    1.图解MapReduce 2.简历过程: Input: Hello World Bye World Hello Hadoop Bye Hadoop Bye Hadoop Hello Hadoop M ...

随机推荐

  1. Visual Studio 2017 怎么将自动生成属性设置为旧版格式

    工具:Visual Studio 2017 1.点击工具,进入选项 2.选项窗口左侧找到C#--代码样式,点击 3.找到表达式首选项中:使用属性的表达式主体.使用索引器的表达式主体和使用访问器的表达式 ...

  2. Javaweb编程中的乱码问题

    程序中的乱码问题,主要出现在我们处理中文数据的过程中出现.从浏览器向服务器请求数据,服务器返回的数据在浏览器中显示为乱码.或者是服务器中的java文件用到中文,也有可能会出现乱码.数据库在处理数据的时 ...

  3. Python链接Mssql之Python库pymssql

    连接数据库 pymssql连接数据库的方式和使用sqlite的方式基本相同: 使用connect创建连接对象 connect.cursor创建游标对象,SQL语句的执行基本都在游标上进行 cursor ...

  4. Spring中属性注入的几种方式以及复杂属性的注入

    在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...

  5. Chapter 5 Blood Type——3

    Disappointment flooded through me as my eyes unerringly focused on his table. 当我的眼睛完全集中在他的桌上时,失望如洪水般 ...

  6. nginx普通配置/负载均衡配置/ssl/https配置

    1.nginx普通配置 server { listen ; server_name jqlin.lynch.com; access_log /var/log/nginx/main.log main; ...

  7. spring框架应用系列四:切面编程(环绕通知与前后置通知区别)

    切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...

  8. leetcode — balanced-binary-tree

    /** * Source : https://oj.leetcode.com/problems/balanced-binary-tree/ * * * Given a binary tree, det ...

  9. 补习系列(6)- springboot 整合 shiro 一指禅

    目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...

  10. C指针和数组的关系详解

    1.C中数组和指针的关系 对于任意类型的数组arr,对于同类型的指针类型parr(确切一点,可以假设类型为int,即int arr[], *parr).它们之间有如下"内幕": 1 ...