C# 读取大文件 (可以读取3GB大小的txt文件)

在处理大数据时,有可能 会碰到 超过3GB大小的文件,如果通过 记事本 或 NotePad++去打开它,会报错,读不到任何文件。

如果你只是希望读取这文件中的前几行,怎么办,下面的工具会帮您解决这个问题. 而且读取时间很快。

截图:


工具下载地址: http://pan.baidu.com/s/1y34wt      (15KB左右, 备注:要运行这个工具,需要您的机器已装过 .netFramework4.0 )

源代码下载地址:http://pan.baidu.com/s/1jAyjl        (70KB左右)



源代码样例:

1,读取一般文件的代码

public static string ReaderFile(string path)
        {
            string fileData = string.Empty;
            try
            {   ///读取文件的内容    
                StreamReader reader = new StreamReader(path, Encoding.Default);
                fileData = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception ex)
            {
                // throw new Exception(ex.Message,ex);  
            }  ///抛出异常    
            return fileData;
        }


2, 读取 大文件(大到约4个GB的文本文件)

private bool ReadBigFile()
        {
            string sTmpFile=@"c:\tmpTest.txt";
            if (File.Exists(sTmpFile))
            {
                File.Delete(sTmpFile);
            }

            if (!System.IO.File.Exists(sTmpFile))
            {
                FileStream fs;
                fs = File.Create(sTmpFile);
                fs.Close();
            }

            if (!File.Exists(txtFileName.Text.Trim()))
            {
                lblResult.Text = "File not exist!";
                txtFileName.Focus();
                return false;
            }

            FileStream streamInput = System.IO.File.OpenRead(@txtFileName.Text.Trim());
            FileStream streamOutput = System.IO.File.OpenWrite(sTmpFile);

            int iRowCount = 10;
            int.TryParse(txtRowCount.Text.Trim(), out iRowCount);

            try
            {
                for (int i = 1; i <= iRowCount; )
                {
                    int result = streamInput.ReadByte();
                    if (result == 13)
                    {
                        i++;
                    }
                    if (result == -1)
                    {
                        break;
                    }
                    streamOutput.WriteByte((byte)result);
                }
            }
            finally
            {
                streamInput.Dispose();
                streamOutput.Dispose();
            }

            string sContent = ReaderFile(sTmpFile);
            CopyToClipboard(sContent);

            return true;
        }

        public static string ReaderFile(string path)
        {
            string fileData = string.Empty;
            try
            {   ///读取文件的内容    
                StreamReader reader = new StreamReader(path, Encoding.Default);
                fileData = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception ex)
            {
                // throw new Exception(ex.Message,ex);  
            }  ///抛出异常    
            return fileData;
        }

        private void CopyToClipboard(string sSource)
        {
            Clipboard.Clear();
            if (!string.IsNullOrEmpty(sSource))
            {
                Clipboard.SetText(sSource);
            }
        }


  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值