C# 使用streamReader.ReadBlock(缓冲区)导入文件

C# 使用streamReader.ReadBlock(缓冲区)导入文件,c#,buffer,streamreader,readblock,C#,Buffer,Streamreader,Readblock,需要导入大量文本文件并查找一些研究资料,特别是针对我的问题,我决定在这里发布解决方案。我相信它会帮助别人 我的档案是300万以上的登记册。尝试使用StreamReader.ReadLine()逐行读取,但不切实际。此外,文件太大,无法加载到内存中 解决方案是使用streamReader.ReadBlock()以块(缓冲区)的形式加载内存中的文件 我遇到的困难是ReadBlock()逐字节读取,以一行或另一半的形式出现。然后,第一行的下一个缓冲区不完整。为了更正,我加载一个字符串(resto)并连

需要导入大量文本文件并查找一些研究资料,特别是针对我的问题,我决定在这里发布解决方案。我相信它会帮助别人

我的档案是300万以上的登记册。尝试使用StreamReader.ReadLine()逐行读取,但不切实际。此外,文件太大,无法加载到内存中

解决方案是使用streamReader.ReadBlock()以块(缓冲区)的形式加载内存中的文件

我遇到的困难是ReadBlock()逐字节读取,以一行或另一半的形式出现。然后,第一行的下一个缓冲区不完整。为了更正,我加载一个字符串(resto)并连接到下一个缓冲区的第1行(primeiraLinha)

使用拆分的另一个重要细节是,在大多数示例中,变量的第一次验证之后是Trim(),以消除空格。在这种情况下,我不使用,因为它加入了第1行和第2行缓冲区

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
        {
            const string arquivo = "Arquivo1.txt";
            using (var streamReader = new StreamReader(arquivo))
            {
                int deslocamento = 1000;
                int pStart = 0; // buffer starting position
                int pEnd = deslocamento; // buffer end position
                string resto = ""; 
                for (int i = pStart; i < int.MaxValue; i += pStart)
                {
                    string primeiraLinha;
                    char[] buffer = new char[pEnd-pStart];
                    streamReader.ReadBlock(buffer, 0, buffer.Length);
                    var bufferString = new String(buffer);
                    string[] bufferSplit = null;
                    bufferSplit = bufferString.Split(new char[] { '\n' });
                    foreach (var bs in bufferSplit )
                    {
                        if (bs != "")
                        {
                            if (resto != "")
                            {
                                primeiraLinha = resto + bs;
                                Console.WriteLine(primeiraLinha);
                                resto = "";
                            }
                            else
                            {
                                if (bs.Contains('\r'))
                                {
                                    Console.WriteLine(bs);
                                }
                                else
                                {
                                    resto = bs;
                                }
                            }
                        }
                    }
                    Console.ReadLine();
                    // Moves pointers
                    pStart = pEnd;
                    pEnd += deslocamento;
                    if (bufferString == null)
                        break;
                }
            }
        }
    }
}
使用系统;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序2
{
班级计划
{
静态void Main()
{
常量字符串arquivo=“Arquivo1.txt”;
使用(var streamReader=newstreamreader(arquivo))
{
int deslocamento=1000;
int pStart=0;//缓冲区起始位置
int pEnd=deslocamento;//缓冲区结束位置
字符串resto=“”;
对于(int i=pStart;i
我的朋友加布里埃尔·古斯塔夫(Gabriel Gustaf)给了我很大的帮助,帮助我解决了这个问题

如果有任何人对进一步提高性能有任何建议,或提出任何意见,请随意。

C#设计一个用于处理大型文件的类:。这很简单,我想可以帮助你