簡體   English   中英

在C#中按塊讀取文本文件的最佳方法

[英]Best way to read a text file by chunks in C#

我需要閱讀一個大文本文件,並在每一行中搜索一個字符串,每行之間用換行符分隔,並且我需要最小化I / O和RAM

我的想法是將文件分成多個塊,因此我有兩種方法:

1)用類似這樣的方法拆分FileStream,但是我冒着將文本行切成兩半的風險,這會使事情變得復雜:

 using (FileStream fsSource = new FileStream("InputFiles\\1.txt", FileMode.Open, FileAccess.Read))
            {
                // Read the source file into a byte array.
                int numBytesToRead = 1024; // Your amount to read at a time
                byte[] bytes = new byte[numBytesToRead];

                int numBytesRead = 0;
                while (numBytesToRead > 0)
                {
                    // Read may return anything from 0 to numBytesToRead.
                    int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

                    // Break when the end of the file is reached.
                    if (n == 0)
                        break;

                    //done something with the lines here.
                }
            }

2)創建一個擴展方法,將行列表分成較小的行列表,然后在每行中搜索單詞,但是我不確定該方法如何影響I / O和RAM!

public static IEnumerable<IEnumerable<TValue>> Chunk<TValue>(this IEnumerable<TValue> values, int chunkSize)
        {
            using (var enumerator = values.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    yield return GetChunk(enumerator, chunkSize).ToList();
                }
            }
        }

        private static IEnumerable<T> GetChunk<T>(IEnumerator<T> enumerator, int chunkSize)
        {
            do
            {
                yield return enumerator.Current;
            } while (--chunkSize > 0 && enumerator.MoveNext());
        }

有什么想法或其他方法可以使用嗎?

提前致謝。

我認為您太過復雜了。 當您要閱讀文本文件時,NET框架有很多方法可供選擇。

如果您需要處理一個大文本文件,則最好使用File.ReadLines方法,因為它不會加載內存中的所有文件,但可以逐行處理

正如您可以從MSDN文檔中閱讀的

當您使用ReadLines時,可以在返回整個集合之前開始枚舉字符串的集合。

foreach(string line in File.ReadLines(@"InputFiles\1.txt"))
{
    // Process your line here....
}

使用File.ReadLines方法,因為它將一次將一行讀入內存,並且您可以在那一行上執行一些邏輯。

foreach(var thisLine in File.ReadLines("path"))
{
    if(thisLine.Contains("Something"))
    {
        // Do something
    }
}

暫無
暫無

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

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