当前位置: 移动技术网 > IT编程>开发语言>Java > WordCount统计文档字符数,单词数,行数

WordCount统计文档字符数,单词数,行数

2018年10月22日  | 移动技术网IT编程  | 我要评论

身残心不残打一字,今日金价多少一克,keen官网

一、项目简介

       源码地址: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

package com.jie;

/**
 * @author: jie140
 * @date: 2018/10/20 20:59
 */
public class wc {
    //定义字符
    public int chars;
    //定义单词
    public int words;
    //定义行数
    public int lines;

    public int getchars() {
        return chars;
    }

    public int getwords() {
        return words;
    }

    public int getlines() {
        return lines;
    }

    public wc(int chars, int words, int lines) {
        this.chars = chars;
        this.words = words;
        this.lines = lines;
    }
}

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

package com.jie;

import java.io.bufferedwriter;
import java.io.file;
import java.io.filewriter;
import java.io.ioexception;
import java.util.arraylist;

public class main {
    public static string inputfile; //输入文件
    public static string outputfile; //输出字符文件
    public static boolean needc; //判断是否统计字符数  -c
    public static boolean needw; //判断是否统计单词数  -w
    public static boolean needl; //判断是否统计行数 -l
    public static boolean needo; //判断是否输出 -o
    public static void main(string[] args)
    {
        //初始化输入文件
        inputfile="d:\\项目\\qnmd.txt";
        for(int i=0;i<args.length;i++)
        {
            system.out.println(args[i]);
            if ("-c".equals(args[i])) {
                needc = true;
            } else if ("-w".equals(args[i])) {
                needw = true;
            } else if ("-l".equals(args[i])) {
                needl = true;
            } else if ("-o".equals(args[i])) {
                needo = true;
                //-o紧挨着输出文件名
                outputfile = args[i + 1];
            } else {
                //如果遇到文件名参数,且前面不是-e和-o,那么这个文件就是输入文件
                if (!args[i - 1].equals("-e") && !args[i - 1].equals("-o")) {

                    inputfile = args[i];
                }
            }

        }
        string outputstr="";
        //输入文件列表
        arraylist<string> filenames =new arraylist<string>();
        filenames.add(inputfile);
        //文件数量
        int len=filenames.size();
        string fn;

        for(int i=0;i<len;i++)
        {
            fn=filenames.get(i);
            system.out.println(fn);
            //分割出实际文件名
            string fileshortname=fn.substring(fn.lastindexof("\\")+1, fn.length());

            if(needc||needw||needl)
            {
                wc wc= statisticalcount.basicinfo(fn);

                if(needc)
                {
                    outputstr+=fileshortname;
                    outputstr+=", char: ";
                    outputstr+=wc.getchars();
                    outputstr+="\r\n";
                }
                if(needw)
                {
                    outputstr+=fileshortname;
                    outputstr+=", word: ";
                    outputstr+=wc.getwords();
                    outputstr+="\r\n";
                }
                if(needl)
                {
                    outputstr+=fileshortname;
                    outputstr+=", line: ";
                    outputstr+=wc.getlines();
                    outputstr+="\r\n";
                }
            }

        }

        system.out.println(outputstr);
        if(!needo)
        {
            //如果没有给输出参数,输出到默认文本中
            outputfile="result.txt";
        }
        try
        {
            //否则定义新文件,输出到新文件中
            file writename = new file( outputfile);
            writename.createnewfile();
            bufferedwriter out = new bufferedwriter(new filewriter(writename));
            out.write(outputstr);
            out.flush();
            out.close();
        }
        catch (ioexception e)
        {
            e.printstacktrace();
        }


    }

}

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

package com.jie;


import java.io.*;

/**
 * @author: jie140
 * @date: 2018/10/20 20:59
 */
public class statisticalcount {
    public static wc basicinfo(string filename)
    {
        //初始化文件的系数值
        wc wc=new wc(0,0,0);
        //当前字符
        char charnow;
        try
        {
            //创建文件并读取
            file filename = new file(filename);
            inputstreamreader reader = new inputstreamreader(
                    new fileinputstream(filename));
            bufferedreader br = new bufferedreader(reader);
            //读取一行的数据
            string line ;
            line = br.readline();
            //是否分割
            boolean partition=true;
            while (line != null)
            {

                //获取一行中字符长度
                wc.chars+=line.length();
                wc.lines++;


                for(int i=0;i<line.length();i++)
                {
                    //当前字符
                    charnow=line.charat(i);
                    //分割出单词
                    if(partition==true&&charnow!=' '&&charnow!='\t'&&charnow!=','&&charnow!=',')
                    {
                        wc.words++;
                    }
                    //分割
                    if(charnow==' '||charnow=='\t'||charnow==','||charnow==',')
                    {
                        partition=true;
                    }

                }
                line = br.readline();
            }
            //加上回车长度
            wc.chars+=wc.lines-1;
            br.close();
        }
        catch (ioexception e)
        {
            e.printstacktrace();
        }
        return wc;
    }
}

三、测试项目

    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知识和文件操作,不了解的小伙伴请多参考几篇博客进行知识补充。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网