如何从目录(具有不同扩展名)中读取所有文件,以及如何分别为每个文件编写报告

时间:2019-06-16 10:22:09

标签: c#

我想从具有不同扩展名的目录中读取所有文件,例如.cpp,.cs,.java并计算其中的总单词数以及特定单词的出现次数。程序应显示每个文件的结果,并使用该文件名为其写报告。

我的代码是从目录中仅读取一个指定的文件,并将报告写入具有我指定名称的.txt文件中。

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;


class WordCounter
{

    public static double assertPercentage;
    public static int assertCount = 0;
    static void Main()
    {
        int asserts=0 ;

        char[] delimiters = { ' ', '.', ',', ';', '\'', '-', ':', '!', '?', '(', ')', '<', '>', '=', '*', '/', '[', ']', '{', '}', '\\', '"', '\r', '\n', '_' };

        string inFileName = "D:\\AssertCount\\testData.txt";
        StreamReader sr = new StreamReader(inFileName);
        string text = System.IO.File.ReadAllText(@"D:\\AssertCount\\testData.txt");
        Regex reg_exp = new Regex("[^a-zA-Z0-9]");
        text = reg_exp.Replace(text, " ");

        string[] words = text.Split(new char[] {
            ' '
        }, StringSplitOptions.RemoveEmptyEntries);

        var word_query = (from string word in words orderby word select word).Distinct();
        string[] result = word_query.ToArray();
        int totalWords = 0;

        //string delim = " ,.";
        string[] fields = null;
        string line = null;
        while (!sr.EndOfStream)
        {

            line = sr.ReadLine(); //each time you read a line you should split it into the words  
            line.Trim();
            fields = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            totalWords += fields.Length; //and just add how many of them there is  
            foreach (string word in result)
            {

               CountStringOccurrences(text, word);
            }
        }
        foreach (string word in result)
        {
            asserts = CountAssertOccurence(text, word);
        }

        sr.Close();
        Display(totalWords, assertCount);
        Console.ReadLine();
    }



   //count frequency of assertions
    public static int CountAssertOccurence(string text, string word)
    {

        int i = 0;

        while ((i = text.IndexOf(word, i)) != -1)
        {
            i += word.Length; 
                if (word == "assert")
                {
                    assertCount++;

                }
        }
        return assertCount;
    }


    //Count the frequency of each unique word.  
    public static void CountStringOccurrences(string text, string word)
    {
        int count = 0;
        int i = 0;
        while ((i = text.IndexOf(word, i)) != -1)
        {
            i += word.Length;
            count++;
        }
    }
    //displays results at the end 
    public static void Display(int totalWords, int assertCount)
    {
        Console.WriteLine("******************************************");
        Console.WriteLine("The total word count is {0}", totalWords);
        Console.WriteLine("Assert count is " + assertCount);
        assertPercentage = (assertCount * 100) / totalWords;
        Console.WriteLine("Occurence of assert keyword in code is  " + assertPercentage + "%");

        //call for reportWriting function
        ReportWriting(totalWords, assertCount, assertPercentage);
    }
    //report writing method
    public static void ReportWriting(int totalWords, int assertCount, double assertPercentage)
    {
        StreamWriter file = new StreamWriter("d://AssertCount//Reports//result.txt");
        file.Write("TotalWords = " + totalWords);
        file.Write("\nAasserts = " + assertCount);
        file.Write("\nOccurence of assert keyword in code is " + assertPercentage + " % ");
        file.Close();

    }
}

我想要的所有其他功能正在运行。我只希望我的程序从文件夹中读取所有文件,并分别为每个文件编写报告。 我知道互联网上有方法,但是我无法实现。

0 个答案:

没有答案