软件工程第三个程序:“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();
}
}
}

Java编写的程序 可以在cmd.exe里调用运行,提升一下bigger,输出的结果如图:

桌面上123.txt的内容如下:

using System;

namespace OneProgram
{ class Program
{
Console.Write("wc.exe");
}
}

拓展功能

参数 -s 的使用效果为:参数中带有“-s”,文件的名称为“*.XXX”,输出所有的.XXX文件的信息。

相当于先找到所有该目录下的文件,然后判断文件的类型,再把符合的文件进行文件信息统计,最后输出。

那就直接再写一个ResponseforS()函数来专门处理参数带“-s”的命令,首先记录其他参数,分离文件路径 与 文件类型;定义字符串数组记录文件路径下的所有文件名;与用户输入的文件类型比较,对符合的文件进行文件信息统计,输出;循环 比较 输出;

    private static void ResponseforS() {
// TODO Auto-generated method stub
String path = sFilename.substring(0,sFilename.indexOf("*")-1);
String type = sFilename.substring(sFilename.indexOf("*")+1,sFilename.length()); System.out.print("Path:");
System.out.println(path + "\n"); File dir = new File(path); if(dir.isDirectory()) {
File next[] = dir.listFiles();
for (int i = 0; i < next.length; i++) {
if( ! next[i].isDirectory()) {
//System.out.println(next[i].getName());
if(next[i].getName()
.substring(next[i].getName().indexOf(".")
,next[i].getName().length())
.equals(type)) { System.out.println(next[i].getName());
sFilename = path + "\\" + next[i].getName();
BaseCount();
Response();
}
}
}
}
else{
System.out.println("path error");
}
}

输出的结果如图:

参数 -a 的使用效果为:参数中带有“-a”,输出文件的空行、码行、注释行的统计信息。

该功能与基本功能类似,直接在BaseCount()函数里改写,函数名改为BaseAndAdvanceCount()。

在程序对文件读行的时候判断该行的长度,判断是否为空行,判断是否有连续的“/”,来判断是否为注释行,如果既不是空行 也不是注释行,则为代码行。

    private static void BaseAndAdvanceCount() {

        linecount = 0;
wordcount = 0;
charcount = 0; nullLinecount = 0;
codeLinecount = 0;
noteLinecount = 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(); line = line.trim();
// 空白行
if (line == "" || line.length() <= 1) {
nullLinecount++;
continue;
}
// 注释行
int a = line.indexOf("/");
int b = line.substring(a+1).indexOf("/");
if ( b == 0 ) { noteLinecount++;
continue;
}
// 代码行
codeLinecount++;
} wordcount = sb.toString().split("\\s+").length;// br.close();
isr.close();
fis.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("path error");
}
}

输出的结果如图:

桌面上123.txt里的内容改为:

using System;

namespace OneProgram
{//
//
class Program
{
Console.Write("wc.exe");
}
}//without copyright

高级功能

参数 -x 的使用效果为:只输入“-x”,弹出文件选择对话框,选择文件后输出该文件的所有信息。

这里仍然为参数 “-x” 建立一个独立的函数ResponseforX(),创建显示一个文件选择对话框,选中某个文件后返回该文件的绝对路径,交给BaseAndAdvanceCount()函数 与 Response()函数。输出所有信息。

    private static void ResponseforX() {
// TODO Auto-generated method stub flag_x = 1;
chooser = new JFileChooser();
int value = chooser.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
sFilename = file.getAbsolutePath();
BaseAndAdvanceCount();
Response();
}
}

操作过程与输出的结果如图:

桌面上123.txt里的内容没变。

至此所有的功能已基本实现 :)

所有的代码已上传至Coding,Coding:https://coding.net/u/Mr_winter/p/ESE03/git

>>>追加

问题1.参数的顺序是固定的么?比如 -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。:)

问题2.如果要想处理/* */ 这样的注释,该如何修改代码呢?

回答:改一下代码呗

    private static void BaseAndAdvanceCount() {

        int flag_note = 0;// 为1时 说明程序正在读/* */中的内容

        linecount = 0;
wordcount = 0;
charcount = 0; nullLinecount = 0;
codeLinecount = 0;
noteLinecount = 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();
// ++++++++++++++++++修改区域++++++++++++++++++++
line = line.trim();
if (flag_note == 0) {
// 空白行
if (line == "" || line.length() <= 1) { nullLinecount++;
continue;
}
// 注释行
int a = line.indexOf("/");
int b = line.substring(a + 1).indexOf("/");
if (b == 0) { noteLinecount++;
continue;
} else {
int c = line.substring(a + 1).indexOf("*");
if (c == 0) { flag_note = 1;
noteLinecount++;
continue;
}
}
// 代码行
codeLinecount++;
} else {
// 统计/* */的大小
int a = line.indexOf("*");
int b = line.substring(a + 1).indexOf("/");
if (b == 0) { noteLinecount++;
flag_note = 0;// /* */结束,标志重新置0
} else { noteLinecount++;
}
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++
wordcount = sb.toString().split("\\s+").length;// br.close();
isr.close();
fis.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("path error");
}
}

运行的结果让人很满意 :)

123.txt 中的内容为:

using System;

namespace OneProgram
{//
//
class Program
/* {
Console.Write("wc.exe");
}
}//without copyright*/

:)

J.X.Dinosaur

软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序的更多相关文章

  1. c#词频统计命令行程序

    这里将用c#写一个关于词频统计的命令行程序. 预计时间分配:输入处理3h.词条排序打印2h.测试3h. 实际时间分配:输入处理1h.词条排序打印2h.测试3h.程序改进优化6h. 下面将讲解程序的完成 ...

  2. 2019-11-29-dotnet-使用-System.CommandLine-写命令行程序

    title author date CreateTime categories dotnet 使用 System.CommandLine 写命令行程序 lindexi 2019-11-29 08:33 ...

  3. 2019-8-31-dotnet-使用-System.CommandLine-写命令行程序

    title author date CreateTime categories dotnet 使用 System.CommandLine 写命令行程序 lindexi 2019-08-31 16:55 ...

  4. dotnet 使用 System.CommandLine 写命令行程序

    在写命令行程序的时候,会遇到命令行解析的问题,以及参数的使用和规范化等坑.现在社区开源了命令行项目,可以帮助小伙伴快速开发命令行程序,支持自动的命令行解析和规范的参数 我写过一篇关于命令行解析的博客C ...

  5. 在 Mac OS X 上创建的 .NET 命令行程序访问数据库 (使用Entity Framework 7 )

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  6. myapp——自动生成小学四则运算题目的命令行程序(侯国鑫 谢嘉帆)

    1.Github项目地址 https://github.com/baiyexing/myapp.git 2.功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 功能(已全部实现) 使用 -n ...

  7. 用什么库写 Python 命令行程序?看这一篇就够了

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  8. XP下,文件夹添加右键命令行

    原文:XP下,文件夹添加右键命令行 总共有3种方式: --------------------1---------------------------------------------------- ...

  9. 命令行程序增加 GUI 外壳

    Conmajia © 2012 Updated on Feb. 21, 2018 命令行大家都用过: 图 1 命令行程序工作界面 现在想办法为它做一个 GUI 外壳,实际效果参考图 2. 图 2 带 ...

随机推荐

  1. 阿里云ECS专有网络下安装flannel注意事项

    参照文章http://www.cnblogs.com/lyzw/p/6016789.html在两台阿里云ECS主机安装与配置flannel,在专有网络下两台主机只能通过公网ip连通,所以flannel ...

  2. [UE4]蓝图节点的组织

    1.将选择的多个蓝图节点变成一个节点,可以给这个节点命名:还可以随时展开这个节点 2.也可以将选中的蓝图节点转换成一个函数或者一个宏.当然也是可以随时展开成原来的样子. 3.变成节点的话,会生成一个子 ...

  3. [UE4]碰撞的随机性

    物理引擎(包括碰撞)的计算具有随机性 原因: 一.每一帧的时间并不是严格相等 二.浮点数计算不是完全准确(两个浮点数运算,结果不可重复) 影响 在左边窗口(服务器端)打几发子弹把其中3个立方体的位置打 ...

  4. SCCM2012 R2实战系列之九:OSD(中)--捕获镜像

    在上篇文章中我们详细的完成了OSD的初始化配置.导入镜像.任务序列的创建和常见问题的排错.但是在实际环境中这样分发了干净的操作系统后还需要手动为客户端安装各种各样的应用程序.所以更为好的方法是将一台计 ...

  5. 数据迁移_把RAC环境备份的数据,恢复到另一台单机Oracle本地文件系统下

    数据迁移_把RAC环境备份的数据,恢复到另一台单机Oracle本地文件系统下 作者:Eric 微信:loveoracle11g 1.创建pfile文件 # su - ora11g # cd $ORAC ...

  6. Delphi XE5中的新增内容

    Delphi XE5中的新增内容 Delphi XE5是所有Delphi开发人员的必须备升级,并且是来自Embarcadero的获奖的.多设备应用开发解决方案的最新版本.使用Delphi XE5的新特 ...

  7. Node JS 8 如何在浏览器上在线调试

    0:为何专门针对Node8写这个 从nodejs8开始,node去掉了_debugger , 内部集成了inspect , 以往使用node-inspect实现的在线调试不再可用.node8开始要用新 ...

  8. 《linux性能及调优指南》 3.3 内存瓶颈

    摘要:3.3内存瓶颈OnaLinuxsystem,manyprogramsrunatthesametime.Theseprogramssupportmultipleusers,andsomeproce ...

  9. SpringBoot应用部署到Tomcat中无法启动问题(初识)

    参考http://blog.csdn.net/asdfsfsdgdfgh/article/details/52127562 背景 最近公司在做一些内部的小型Web应用时, 为了提高开发效率决定使用Sp ...

  10. Mac下RabbitMQ安装和在Java client端的使用

    安装: 1.使用homebrew下载rabbitMQ: brew install rabbitmq 执行结果如下: Updating Homebrew... ==> Auto-updated H ...