Word Count

“WC项目” —— 文件信息统计(Word Count ) 命令行程序

格式:wc.exe [parameter][filename]

在[parameter]中,用户通过输入参数与程序交互,需实现的功能如下:

1、基本功能

支持 -c 统计文件字符数
支持 -w 统计文件单词数
支持 -l 统计文件总行数



2、拓展功能

支持 -a 返回高级选项(代码行 空行 注释行)
支持 -s 递归处理符合条件的文件

3、高级功能

支持 -x 程序以图形界面与用户交互

[filename] 是待处理文件名。

 

 

 



基本功能

主函数里用String字符串接收用户输入,并分解成参数数组 和 文件地址。

编写BaseCount()函数实现对文件的读操作,逐行统计统计字符数,并记录行数,同时使用StringBuffer类记录文件中所有的信息。最后一起统计 词数,避免在逐行计词时,把标点计为一词。

编写Response()函数根据用户输入的参数输出信息,这里程序不管用户输入的参数是什么,都在读取文件的时候把所有信息都记录下来并保存,用户输入的参数里有什么输出什么。

源代码:

private static void BaseCount() {
        
        linecount = 0;
        wordcount = 0;
        charcount = 0;
        
        File file = new File(sFilename);
        if(file.exists()) {
            try {

                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
                BufferedReader br = new BufferedReader(isr);

                String line = "";
                StringBuffer sb  = new StringBuffer();

                while ((line = br.readLine()) != null)
                {
                    linecount++;
                    sb.append(line);
                    charcount += line.length();
                }

                wordcount = sb.toString().split("\\s+").length;//
                                
                br.close();
                isr.close();
                fis.close();
                                                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }






 

查询出的一些问题

问题.参数的顺序是固定的么?比如 -c 和 -w 的顺序是否可以交换?

回答:可以的,粘出Response()函数的部分代码如下

for (int i = 0; i < sParameter.length; i++) {
                if (sParameter[i].equals("-l")) {
                    System.out.print("line count:");
                    System.out.println(linecount);
                } else if (sParameter[i].equals("-w")) {
                    System.out.print("word count:");
                    System.out.println(wordcount);
                } else if (sParameter[i].equals("-c")) {
                    System.out.print("char count:");
                    System.out.println(charcount);
                } else if (sParameter[i].equals("-a")) {
                    System.out.print("blank lines count:");
                    System.out.println(nullLinecount);
                    System.out.print("code lines count:");
                    System.out.println(codeLinecount);
                    System.out.print("note lines count:");
                    System.out.println(noteLinecount);
                }



先输入 -c 就先输出 char count ;先输入 -w 就先输出 word count。:)


还在进一步自学中,还有一大把时光让我去掌握,继续努力!Go on!

posted @ 2017-11-04 21:11  昕格尔  阅读(412)  评论(0编辑  收藏  举报