CMD文件内容统计程序简单版本

WordCount命令行程序通过CMD接收参数,输出统计结果到指定文件。

项目码云地址:https://gitee.com/ggtc/WordCount.git

实现的功能有:

统计文件字符数

 1 using System.IO;
 2 
 3 namespace WordCount
 4 {
 5      public class ClassCharCount:InterfaceCommandable
 6     {
 7          public string Count(string fileName)
 8          {
 9              string strResult = "字符数:";
10              try
11              {
12                  FileStream fs = new FileStream(fileName, FileMode.Open);
13                  strResult += Convert.ToString(fs.Length);
14                  fs.Close();
15                  Console.WriteLine("字符数统计成功");
16                  return strResult;
17              }
18              catch
19              {
20                  Console.WriteLine("文件打开失败");
21                  return strResult;
22              }           
23          }
24     }
25 }

 

 

 

统计单词个数(逗号,空格分隔)

 1 using System.IO;
 2 
 3 namespace WordCount
 4 {
 5     class ClassWordCount:InterfaceCommandable
 6     {
 7         public string Count(string fileName)
 8         {
 9             string strResult = "单词数:";
10             try
11             {
12                 FileStream fs = new FileStream(fileName, FileMode.Open);
13                 StreamReader sr = new StreamReader(fs, Encoding.Default);
14                 string sen = Convert.ToString(sr.ReadToEnd());
15                // Console.WriteLine(sen);//检查读文件结果
16                 fs.Close();
17                 sr.Close();
18                 char[] separator={',',' ',''};//中英文逗号及空格
19                 string[] words = sen.Split(separator, StringSplitOptions.RemoveEmptyEntries);//按指定字符分割字符串
20                 strResult += Convert.ToString(words.Length);
21                 Console.WriteLine("单词数统计成功");
22                 return strResult;
23             }
24             catch
25             {
26                 Console.WriteLine("文件打开失败");
27                 return strResult;
28             }       
29         }
30 
31        /* public string DeleteComments(string fs)//去注释
32         {
33             string subString = "";
34             int[] comments,
35             return subString;
36         }*/
37     }
38 }

 

 

统计文件行数(换行符为准)

 

 1 using System.IO;
 2 
 3 namespace WordCount
 4 {
 5     class ClassRowsCount:InterfaceCommandable
 6     {
 7         public string Count(string fileName)
 8         {
 9             string strResult = "行数:";
10             try
11             {
12                 FileStream fs = new FileStream(fileName, FileMode.Open);
13                 StreamReader sr = new StreamReader(fs, Encoding.Default);
14                 string sen = Convert.ToString(sr.ReadToEnd());
15                 // Console.WriteLine(sen);//检查读文件结果
16                 fs.Close();
17                 sr.Close();
18 
19                 char[] separator = { '\n'};
20                 string[] Rows = sen.Split(separator, StringSplitOptions.RemoveEmptyEntries);//按指定字符分割字符串
21                 strResult += Convert.ToString(Rows.Length);
22                 Console.WriteLine("行数统计成功");
23                 return strResult;
24             }
25             catch
26             {
27                 Console.WriteLine("打开文件失败");
28                 return strResult;
29             }
30         }
31     }
32 }

以上三个类实现这个接口

1 interface InterfaceCommandable
2     {
3         string Count(string fileName);
4     }

 

更改默认输出文件

 1  public static void ChangePrint(string fileName)
 2         {
 3             try
 4             {
 5                 StreamWriter sw = new StreamWriter("resultposition.txt");
 6                 sw.Write(fileName);
 7                 sw.Close();
 8                 Console.WriteLine("输出地址更改成功");
 9             }
10             catch
11             {
12                 Console.WriteLine("输出地址更改失败");
13             }
14             
15         }
 1 //用于在模块间传递数据
 2             string strResult = "";
 3             //将输入传给处理模块
 4             strResult = ClassCountDeal.CountDeal(args);
 5             //根据输出地址调用输出模块将统计结果输出到指定文件
 6             StreamReader sr = new StreamReader("resultposition.txt");
 7             string position=sr.ReadToEnd();
 8             sr.Close();
 9             if(position=="")
10             {
11                 OutClass.Print(strResult);
12             }
13             else
14             {
15 
16                 OutClass.Print(strResult, position);
17             }

输出模块

仅仅接收统计结果输出到指定文件

 1 using System.IO;
 2 
 3 namespace WordCount
 4 {
 5     public static class OutClass
 6     {
 7         public static void Print(string strResult)
 8         {
 9                 try
10                     {
11                           StreamWriter sw = new StreamWriter("result.txt");
12                           sw.WriteLine(strResult);//将结果写入默认文件
13                           Console.WriteLine("数据写入成功");
14                           sw.Close();
15                     }
16                 catch
17                     {
18                           Console.WriteLine("数据写入失败");
19                     }
20         }
21 
22         public static void Print(string strResult,string args)
23         {
24             try
25             {
26                 StreamWriter sw = new StreamWriter(args);
27                 sw.WriteLine(strResult);//将结果写入指定文件
28                 Console.WriteLine("数据写入成功");
29                 sw.Close();
30             }
31             catch
32             {
33                 Console.WriteLine("数据写入失败");
34             }
35         }
36 }
37 }

 

根据输入参数调用统计模块

一共有四个命令-c,统计字符数;-w,统计单词数;-l,统计行数;-o,更换输出文件。后面加要统计的文件名。

 1 public static class ClassCountDeal
 2     {
 3         public static string CountDeal(string[] args)
 4         {
 5             string strResult = "";
 6             //根据用户参数个数调用统计模块
 7             int i=0;            
 8             while(i<args.Length-1)
 9             {
10                 switch (args[i])
11                 {
12                     //统计字符数
13                     case "-c":
14                         InterfaceCommandable ccmd = new ClassCharCount();
15                         strResult += ccmd.Count(args[args.Length-1]) + '\n';
16                         break;
17                     //统计单词数
18                     case "-w":
19                         InterfaceCommandable wcmd = new ClassWordCount();
20                         strResult += wcmd.Count(args[args.Length - 1]) + '\n';
21                         break;
22                     //统计行数,换行符为准
23                     case "-l":
24                         InterfaceCommandable lcmd = new ClassRowsCount();
25                         strResult += lcmd.Count(args[args.Length - 1]) + '\n';
26                         break;
27                     case "-o":
28                         //更改输出到用户指定的文件
29                         OutClass.ChangePrint(args[args.Length - 1]);
30                         break;
31                     default:
32                         Console.WriteLine("命令错误");
33                         break;
34                 }
35                 i++;
36             }
37             //返回统计结果
38             return strResult;
39         }
40     }

我通过 strResult 这个变量在各个模块间传输统计数据。

 

附上运行截图在此:

//跳转到程序所在目录

//输入命令和要统计的文件名

结果如下:

测试一下更改输出地址

嗯?看看存放数据文件的名字的记录文件已经改了啊?

 

删除掉地址文件里的数据

 

再试一下写入:

但是

看来运行没错,我的提示语句有了点小小的Bug~

修改判断如下

 

posted @ 2018-09-24 20:27  ggtc  阅读(937)  评论(0编辑  收藏  举报