您当前的位置: 首页 >  c#

txwtech

暂无认证

  • 1浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c#读取超大文本文件-速度快

txwtech 发布时间:2022-01-12 11:36:23 ,浏览量:1

c#读取超大文本文件-速度快

   private string ReadTxtFile(string fileName)
        {

            string txt = "";
            // var fileDir = this.txtFileFolder.Text.Trim();
            var fileDir = fileName;

            byte[] allBytes = null;

            byte[] buffer = new byte[1024];//一个1K的缓冲字节容器
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Restart();
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(fileDir, FileMode.Open, FileAccess.Read))
                {
                    int positon = 0;
                    while ((positon = fs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, positon);

                    }

                    allBytes = ms.ToArray();
                }

            }
            stopwatch.Stop();
            // MessageBox.Show($"StreamReader StreamWriter 程序运行花费的时间:{(double)stopwatch.ElapsedMilliseconds / 1000:0.000} secs");

            if (null != allBytes)
            {
                //尝试将字节转成字符串
                txt = System.Text.Encoding.UTF8.GetString(allBytes);
                // this.richTextBox_Result.Text = txt;

            }
            string[] txtToArray = txt.Split('\r');
            //  ReadData_List.Add(txtToArray);
            return txt;

        }

关注
打赏
1665060526
查看更多评论
立即登录/注册

微信扫码登录

0.0531s