wordcount(代码片段)

zjgss9 zjgss9     2023-01-04     667

关键词:

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

 

二.项目分析:

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

命令格式:

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

基础功能:

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

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

-l:统计文件的行数;

-o:指定输出文件;

三.PSP表格:

 

PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)
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)模块处理方法(通过主方法传递的参数确定输出内容)

import java.io.*;

public class Handle 

    int line = 0;
    int word = 0;
    int charnum = 0;

    public void deal(String readPath,String writePath, String flag) 
        boolean flagexist = true;
        try 
            String str = "";
            String[] linenum;
            File file = new File(readPath);
            BufferedReader br = new BufferedReader(new FileReader(readPath));
            try 
                try 
                    while ((str = br.readLine()) != null) 
                        linenum = str.split(",| ");
                        for (int i = 0; i < linenum.length; i++) 
                            if (linenum[i] != null)
                                word++;
                        
                        line++;
                        charnum += str.length();
                    
                   System.out.println("行数:"+line+ " 单次数:" + word+" 字符数:"+ charnum);
                 catch (FileNotFoundException e) 
                    e.printStackTrace();
                
             catch (IOException e) 
                e.printStackTrace();
             finally 
                try 
                    if (br != null) 
                        br.close();
                    
                 catch (IOException e) 
                    System.out.println("关闭BufferedReader错误");
                
            
         catch (FileNotFoundException e) 
            flagexist = false;
            System.out.println("未找到文件.");
        
        if(!flagexist)

        
        else
            String output = "";
            switch (flag)
                case "-o":
                    output = readPath+",字符数:"+charnum;
                    try
                        File outputFile = new File(writePath);
                        outputFile.createNewFile();
                        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
                        bw.write(output);
                        bw.flush();
                        bw.close();
                    catch (IOException e)
                    
                        e.printStackTrace();
                    
                    break;
                case "-w":
                    output = readPath+",单词数:"+word;
                    try
                        File outputFile = new File(writePath);
                        outputFile.createNewFile();
                        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
                        bw.write(output);
                        bw.flush();
                        bw.close();
                    catch (IOException e)
                    
                        e.printStackTrace();
                    
                    break;
                case "-l":
                    output = readPath+",行数:"+line;
                    try
                        File outputFile = new File(writePath);
                        outputFile.createNewFile();
                        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
                        bw.write(output);
                        bw.flush();
                        bw.close();
                    catch (IOException e)
                    
                        e.printStackTrace();
                    
                    break;
                case "-c":
                    output = readPath+",字符数:"+charnum;
                    try
                        File outputFile = new File(writePath);
                        outputFile.createNewFile();
                        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
                        bw.write(output);
                        bw.flush();
                        bw.close();
                    catch (IOException e)
                    
                        e.printStackTrace();
                    
                    break;
            
        
    

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

import java.io.*;

public class Main 

    public static void main(String[] args) throws IOException 
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String read = null;
        System.out.println("请输入命令(格式:wc.exe [parameter] [input_file_name]):");
        try
                String readPath = "file.c";
                String writePath = "result.txt";
                read = bf.readLine();
              //  System.out.println(read);
                String [] getRead;
                getRead = read.split(" ");
                if(getRead.length == 3)
                    if(getRead[0].equals("wc.exe"))
                        if(getRead[1].equals("-o"))
                            if(getRead[2].endsWith(".txt"))
                                Handle handle = new Handle();
                                writePath = getRead[2];
                                handle.deal(readPath,writePath,"-o");
                            
                            else 
                                System.out.println("命令格式输入错误");
                            
                        
                        else if(getRead[1].equals("-c"))
                            if(getRead[2].endsWith(".c"))
                                Handle handle = new Handle();
                                readPath = getRead[2];
                                handle.deal(readPath,writePath,"-c");
                            
                            else 
                                System.out.println("命令格式输入错误");
                            
                        
                        else if(getRead[1].equals("-w"))
                            if(getRead[2].endsWith(".c"))
                                Handle handle = new Handle();
                                readPath = getRead[2];
                                handle.deal(readPath,writePath,"-w");
                            
                            else 
                                System.out.println("命令格式输入错误");
                            
                        
                        else if(getRead[1].equals("-l"))
                            if(getRead[2].endsWith(".c"))
                                Handle handle = new Handle();
                                readPath = getRead[2];
                                handle.deal(readPath,writePath,"-l");
                            
                            else 
                                System.out.println("命令格式输入错误");
                            
                        
                        else 
                            System.out.println("命令格式输入错误");
                        
                    
                    else
                        System.out.println("可执行文件名输入错误");
                    
                
                else
                    System.out.println("命令输入格式错误.");
                
        catch(Exception e)
            e.printStackTrace();
        
    

六.测试

正常按格式输入命令

技术分享图片

 

错误的输入

技术分享图片

 

技术分享图片

 

技术分享图片

 

技术分享图片

 

技术分享图片

七.参考文献:

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

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

 


wordcount(代码片段)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespaceWordCountclassProgramstaticvoidMain(string[]args)Typetype=newType() 查看详情

wordcount(代码片段)

1.Github项目地址https://github.com/chaindreamjet/WordCount2.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 20 30·Estimate·估计这个任务需要多少时间 20 30Development开发 700 940·Analysis·需求分析(包括 查看详情

mapreduce编写wordcount程序代码实现(代码片段)

MapReduce经典案例代码(wordcount)以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数packagecom.fwmagic.mapreduce;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io. 查看详情

分享知识-快乐自己:运行(wordcount)案例(代码片段)

运行wordcount案例:一):大数据(hadoop)初始化环境搭建二):大数据(hadoop)环境搭建三):运行wordcount案例四):揭秘HDFS五):揭秘MapReduce六):揭秘HBase七):HBase编程-----------------------------------------------------------------Hadoo... 查看详情

wordcount(代码片段)

一.码云地址https://i.cnblogs.com/EditPosts.aspx?opt=1https://gitee.com/lttzy/wordcount二.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 20·Estimate·估计这个任务需要多少时间 20 10Development开发 45 查看详情

wordcount(代码片段)

一.github:https://github.com/JiejieCM/WordCount二.pspPSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 30·Estimate·估计这个任务需要多少时间3030Development开发 3天 5天·Analysis·需求分析(包括学习新技术) 2天&nbs... 查看详情

wordcount(代码片段)

WordCount一.个人Gitee地址:https://gitee.com/godcoder979/(该项目完整代码在这里) 二.项目简介:该项目是一个统计文件字符、单词、行数等数目的应用程序,通过输入命令来执行你想要的操作。所用语言:java命令格式:wc.exe[para]&... 查看详情

wordcount(代码片段)

一、Gitee项目地址https://gitee.com/YSS_MYGit/WordCount二、基本思路 我看到项目后,首先想到的是使用C#语言编写,因为我对C#语言比较熟悉些。此项目有两个类,一个是Program类,该类是程序的入口,主要用于传参,另一个是WC类,... 查看详情

wordcount(代码片段)

一.Gitee地址:https://gitee.com/zjgss99/WordCount 二.项目分析:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。命令格式:wc.exe[para] ... 查看详情

个人项目wordcount(代码片段)

1.项目GitHub地址:https://github.com/FPXBao/wordcount2.解题思路:分析程序基本需求,将其功能分为三个函数调用:主函数intmain();功能函数Ccount();Wcount();Lcount();并进行相关知识学习。3.代码说明:主函数:#include<stdio.h>#include<stdlib.... 查看详情

wordcount(代码片段)

   码云地址:https://gitee.com/qaqxx88/wc1WordCount需求说明 (1)对源文件进行字符数,行数,单词数的统计并且放在与wc.exe相同目录下的result.txt中  (2)基本功能  wc.exe -c file.c   &nbs 查看详情

wordcount基础功能(代码片段)

Gitte地址:https://gitee.com/gyuyue/WordCount PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 15 20·Estimate·估计这个任务需要多少时间 30 30Development开发 60 80·Analysis·需求分析(包括学习新技术)... 查看详情

wordcount代码实现及测试(代码片段)

...者:201631062515201631062415码云地址:https://gitee.com/heshuxiang/WordCount/tree/master2.项目需求  对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多... 查看详情

wordcount基本功能(代码片段)

项目的gitee地址:https://gitee.com/ITtoto/WordCount 开发语言:C语言PSP2.1表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划20 16·Estimate·估计这个任务需要多少时间 30 27Development开发 120 160·Analysis·需... 查看详情

wordcount(代码片段)

   Gitee地址:https://gitee.com/dyh1621838953/WordCount1此项目我只实现了基本功能,即字符、行数、单词数的计算以及将结果输出到文件中。该项目用C#语言进行实现。解题思路:   根据题目要求所知,需要读取一... 查看详情

[mapreduce_1]运行wordcount示例程序(代码片段)

 0.说明  MapReduce实现WordCount示意图&& WordCount代码编写    1. MapReduce实现WordCount示意图     1.Map:预处理阶段,将原始数据映射成每个K-V,发送给reduce  2.Shuffle:混洗(分类),将相同的Key... 查看详情

wordcount(代码片段)

gitee地址:https://gitee.com/yzyindex/wordcount需求分析通过程序设计,编写一个可执行文件exe能够对源程序文件进行统计字符数、单词数、行数,统计结果可以以指定的格式输出到默认文件中。可执行程序命名为:wc.exe,该程序处理用... 查看详情

wordcount-软测作业(代码片段)

1.github地址 https://github.com/zgwe/wordCount2.PSP2.1PSP阶段预估耗时实际耗时(分钟)(分钟)Planning计划 10 10·Estimate·估计这个任务需要多少时间 20 10Development开发 40 60·Analysis·需求分析(包括学习新技术)  查看详情