一、开发者:201631062418
二、代码地址:https://gitee.com/YsuLIyan/WordCount
三、作业地址:https://edu.cnblogs.com/campus/xnsy/Test/homework/2203
四、基本功能
WordCount.exe -c test.txt //返回文件 test.txt的字符数
WordCount.exe -w test.txt //返回文件 test.txt的单词总数
WordCount.exe -l test.txt //返回文件 test.txt的总行数
WordCount.exe -o outputFile.txt //将结果输出到指定文件outputFile.txt
五、扩展功能
-----------------------------------------------------------------------------------------------------------------------------------------------
已经实现:-c -w -l 可以同时使用,如果没有-o命令(或者说-0之后没有接文件名),则默认outputfile.txt作为输出结果的文件(strPath)。并且扫描-0命令之后的接收结果文件名称,如果接收结果文件在该目标文件夹中不存在,则会报路径错误
-----------------------------------------------------------------------------------------------------------------------------------------------
还未实现:接收结果文件在目标文件夹中不存在时,重新创建一个名为(strPath)的接收结果文件--待后期改善。
在命令读取时,程序对于用户输入的命令进行理解、更多的应答以及合适的补充。
-----------------------------------------------------------------------------------------------------------------------------------------------
六、项目的实现
这是一个专门处理命令的方法类,这也是我比较在意的地方,因为我很注重我写的程序对用户输入的参数的应答。
class CommandRead { bool isRows = false; bool isChara = false; bool isWord = false; bool isAimPath = false; string[] path = new string[4]; string[] commands; public bool IsRows { get { return isRows; } } public bool IsChara { get { return isChara; } } public bool IsWord { get { return isWord; } } public bool IsAimPath { get { return isAimPath; } } public string[] Path { get { return path; } } public void CommandImport(string[] args) { commands = args; if(commands == null) { Console.WriteLine("请输入指令"); } else { int Pas = 0; for (int i = 0; i < commands.Length; i++) { if (commands[i] == "-l" || commands[i] == "-L")//行数 { isRows = true; } else if (commands[i] == "-w" || commands[i] == "-W")//单词 { isWord = true; } else if (commands[i] == "-c" || commands[i] == "-C")//字符 { isChara = true; } else if (commands[i] == "-o" || commands[i] == "-O")//输出 { if (i + 2 == commands.Length) isAimPath = true; } else { path[Pas] = commands[i]; Pas++; } } } } }
这是关于读取文件行数的类
class Rows //行数 { public string ope; public int Line(string filename) { Encoding encod = Encoding.GetEncoding("GB2312"); FileStream fs = new FileStream(filename, FileMode.Open); StreamReader sr = new StreamReader(fs, encod); string line; int CountLine = 0;//记录行数 //当前行不为空 while((line = sr.ReadLine()) != null) { CountLine++; } //应该先关闭文件再return sr.Close(); fs.Close(); return CountLine;//注意函数是有返回值的 } }
这是读取文件符号数的类
class Word//字符 { public int TotalWord(string filename) { Encoding encod = Encoding.GetEncoding("GB2312"); FileStream fs = new FileStream(filename, FileMode.Open); StreamReader sr = new StreamReader(fs, encod); int nchar; int WordCount = 0; char[] symbol = { ' ', '\t', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '+', '-', '*', '=' };//间隔符 while((nchar = sr.Read())!= -1) { foreach(char c in symbol) { if(nchar == (int)c) { WordCount++; } } } sr.Close(); fs.Close(); return WordCount; } }
这是读取单词数的类
class Character//单词 { public string ope; public int TotalWord(string filename) { Encoding encod = Encoding.GetEncoding("GB2312"); FileStream fs = new FileStream(filename, FileMode.Open); StreamReader sr = new StreamReader(fs, encod); int charcount = 0; int nchar; while((nchar = sr.Read())!= -1) //sr.Read()是以特定的编码方式读取流而获取一个接一个的字符,而ReadLine()是读取一行 { charcount++; // 统计字符数 } //关闭文件 sr.Close(); fs.Close(); return charcount; } }
这是将得到的信息写入指定文件的方法类
class WriteFile { public void Write(string theword) { FileStream fs = new FileStream("outputfile.txt", FileMode.Create); //获得字节数 byte[] data = System.Text.Encoding.Default.GetBytes(theword); //写入 fs.Write(data, 0, data.Length); //清空缓存区,关闭流 fs.Flush(); fs.Close(); } public void Write(string path,string theword) { bool isInFile = false; String rootPath = Directory.GetCurrentDirectory(); DirectoryInfo root = new DirectoryInfo(rootPath); foreach(FileInfo f in root.GetFiles()) { if(path == f.Name) { isInFile = true; } } if (isInFile != false) { FileStream fs = new FileStream(path, FileMode.Create); //获得字节数 byte[] data = System.Text.Encoding.Default.GetBytes(theword); //写入 fs.Write(data, 0, data.Length); //清空缓存区,关闭流 fs.Flush(); fs.Close(); } else { Console.WriteLine("Path Error!"); } } }
然后就是主函数
class Program { static void Main(string[] args) { string[] cmds = args; string theWord = ""; int ope1 = 0,ope2 = 0,ope3 = 0; for(int i =0; i) Console.WriteLine(args[i]); //新建对象读取命令 CommandRead cmd = new CommandRead(); cmd.CommandImport(cmds); if (cmd.IsRows==true) { Rows rows = new Rows(); ope1 = rows.Line(cmd.Path[0]); Console.WriteLine("Total Line:{0}", ope1); theWord += string.Format("Total Line:{0}\r\n", ope1); } if (cmd.IsChara==true) { Character ch = new Character(); ope2 = ch.TotalWord(cmd.Path[0]); Console.WriteLine("Total Character:{0}", ope2); theWord += string.Format("Total Character:{0}\r\n", ope2); } if (cmd.IsWord==true) { Word word = new Word(); ope2 = word.TotalWord(cmd.Path[0]); Console.WriteLine("Total Word:{0}", ope3); theWord += string.Format("Total Word:{0}\r\n", ope3); } WriteFile write = new WriteFile(); if (cmd.IsAimPath) { write.Write(cmd.Path[1], theWord); } if(cmd.IsAimPath == false) { write.Write(theWord); } } }
七、项目测试
test.txt文件内容:
进行测试
测试结果
测试2
测试结果
心得感悟:其实刚好我在C#方面对于文件的读写这一块并不是很熟练,但是刚好该题就让我去读写文件,而且我并不是很清楚如何在cmd里面调用.exe以及传入参数。