簡體   English   中英

查找文件中字符串的最快方法

[英]Fastest way to find strings in a file

我有一個不超過10KB的日志文件(文件大小最多可達2 MB),我想查找文件中是否至少有一組這些字符串。 這些字符串將在不同的行上,如,

行動:.......

INPUT:...........

結果:..........

如果文件中存在一組以上,我至少需要知道。 我已經做了大約100次測試(每次日志不同,所以我重新加載並閱讀日志),所以我正在尋找最快和下注的方式來做到這一點。

我在論壇中查找找到最快的方法,但我不認為我的文件對於那些問題來說太大了。

Thansk尋找。

我會逐行閱讀並檢查條件。 一旦你看到一個組,你就可以退出。 這樣您就不需要將整個文件讀入內存。 像這樣:

    public bool ContainsGroup(string file)
    {
        using (var reader = new StreamReader(file))
        {
            var hasAction = false;
            var hasInput = false;
            var hasResult = false;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                if (!hasAction)
                {
                    if (line.StartsWith("ACTION:"))
                        hasAction = true;
                }
                else if (!hasInput)
                {
                    if (line.StartsWith("INPUT:"))
                        hasInput = true;
                }
                else if (!hasResult)
                {
                    if (line.StartsWith("RESULT:"))
                        hasResult = true;
                }

                if (hasAction && hasInput && hasResult)
                    return true;
            }
            return false;
        }
    }

此代碼檢查是否有以ACTION開頭的行,然后是INPUT,然后是RESULT。 如果那些順序不重要,那么你可以省略if () else if ()檢查。 如果該行不以字符串開頭,則將StartsWith替換為Contains

這是一種可行的方法:

StreamReader sr;
string fileContents;

string[] logFiles = Directory.GetFiles(@"C:\Logs");

foreach (string file in logFiles)
{

    using (StreamReader sr = new StreamReader(file))
    {

        fileContents = sr.ReadAllText();

        if (fileContents.Contains("ACTION:") || fileContents.Contains("INPUT:") || fileContents.Contains("RESULT:"))
        {
             // Do what you need to here
        }

    }
}

您可能需要根據您的確切實現需求做一些變化 - 例如,如果單詞跨越兩行,該行是否需要以單詞開頭等等。

添加

替代逐行檢查:

StreamReader sr;
string[] lines;

string[] logFiles = Directory.GetFiles(@"C:\Logs");

foreach (string file in logFiles)
{

    using (StreamReader sr = new StreamReader(file)
    {

        lines = sr.ReadAllLines();

        foreach (string line in lines)
        {        
            if (line.Contains("ACTION:") || line.Contains("INPUT:") || line.Contains("RESULT:"))
            {
                // Do what you need to here
            }
        }

    }
}

看看如何從文件中讀取文本 您可能還想查看String.Contains()方法。

基本上你會循環遍歷所有文件。 對於逐行讀取的每個文件,並查看是否有任何行包含1個特殊的“章節”。

在效率方面,您沒有太多關於文本文件的選擇。 最簡單的方法肯定是遍歷每一行數據。 當您在字符串中抓取一行時,將其拆分為空格。 然后將這些單詞與您的單詞匹配,直到找到匹配項。 然后做你需要的任何事情。

我不知道如何在c#中做到這一點,但在vb中它會像...

Dim yourString as string
Dim words as string()
Do While objReader.Peek() <> -1
   yourString = objReader.ReadLine()
   words = yourString.split(" ")
   For Each word in words()
      If Myword = word Then
         do stuff
      End If
   Next
Loop

希望有所幫助

此代碼示例搜索大型文本文件中的字符串。 單詞包含在HashSet中。 它將找到的行寫入臨時文件中。

        if (File.Exists(@"temp.txt")) File.Delete(@"temp.txt");

        String line;
        String oldLine = "";
        using (var fs = File.OpenRead(largeFileName))
        using (var sr = new StreamReader(fs, Encoding.UTF8, true))
        {
            HashSet<String> hash = new HashSet<String>();
            hash.Add("house");
            using (var sw = new StreamWriter(@"temp.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    foreach (String str in hash)
                    {
                        if (oldLine.Contains(str))
                        {
                            sw.WriteLine(oldLine); 
                            // write the next line as well (optional)
                            sw.WriteLine(line + "\r\n");                                    
                        }
                    }
                    oldLine = line;
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM