C#读取大文本文件

  今天偶遇一同事抱怨,sqlserver导出的CSV,明明有1000W条,但用excel打开就只剩100W了,足足消失了90%,所以她怀疑文件是足量的1000W条,是excel捣了鬼。可是文件容量有2G+,用记事本打不开,如何证明CSV文件没有缺少数据,这可难坏了他。

  好吧,本着不看其他轮子,有问题自己造一个的原则,我决定用控制台程序写一个简易读取程序,具体CODE如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BigTextReader
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "";
            do
            {
                Console.WriteLine("Please input the file path:");
                path = Console.ReadLine();
            }
            while (!System.IO.File.Exists(path));
            var fileStream = System.IO.File.OpenRead(path);
            while(true)
            {
                Console.WriteLine("Please input the start position:");
                var position = Int64.Parse(Console.ReadLine());
                if (position == -1)
                {
                    Console.WriteLine("finish");
                    return;
                }
                fileStream.Position = position;
                var byts = new Byte[1000];
                fileStream.Read(byts, 0, 1000);
                var str = Encoding.UTF8.GetString(byts);
                Console.WriteLine(str);
            }

        }
    }
}

好了,程序如上图所示,第一步,输入文件的绝对地址,比如d:a.csv,第二步,输入文本的位置,比如100000,程序默认读取1000个字节作展示。当位置输入为-1时,程序退出。

一个基本的大文本读取器就初见雏形了,用每个ROW的byte数*200W,果然读出了数据,完美的证明了同事的猜想,同时,读取的时间只用了100ms。

PS:个人感觉,Encoding,读取的byte数可以写作配置,但会拖长操作流程,同时,直接Int64.Parse是因为懒,小伙伴们不要效仿哦。

原文地址:https://www.cnblogs.com/Damos/p/bigText.html