欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#利用緩存分塊讀寫(xiě)大文件

 更新時(shí)間:2019年05月04日 10:41:45   作者:Hold人民幣  
這篇文章主要為大家詳細(xì)介紹了C#利用緩存分塊讀寫(xiě)大文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

C#利用緩存分塊讀寫(xiě)大文件,供大家參考,具體內(nèi)容如下

在日常生活中,可能會(huì)遇到大文件的讀取,不論是什么格式,按照儲(chǔ)存文件的格式讀取大文件,就會(huì)在Buffer中看到相關(guān)的文件頭合內(nèi)容, 以一次.txt文件存取為例。

using System.IO;

首先創(chuàng)建demo文件,此處文件大小沒(méi)關(guān)系,只是演示

 private void button2_Click(object sender, EventArgs e)
    {
      using (FileStream fsWrite = new FileStream(@"D:\1.txt", FileMode.Append))
      {
        string temp = "";
        for (int i = 0; i < 10000;i++ )
        {
          temp += i.ToString()+"/t";

        }
        byte [] m = System.Text.Encoding.UTF8.GetBytes (temp);
        fsWrite.Write(m, 0, temp.Length);
      }
    }

讀取創(chuàng)建的文件

private void Readtxt()
{
  using (FileStream fsRead = new FileStream(@"d:\2.txt,FileMode.Open"))
  {
    //剩余文件內(nèi)容長(zhǎng)度
    long leftLength = fsRead.Length;
    //buffersize 
    int buffersize = 1024;
    //創(chuàng)建緩存數(shù)組
    byte[] buffer = new byte[buffersize];
    int rNum = 0;
    int FileStart = 0;
    while(leftLength > 0)
    {
      //設(shè)置文件流的讀取位置
      fsRead.Position = FileStart ;
      if (leftLength < buffersize)
      {
        rNum = fsRead.Read(buffer, 0, Convert.ToInt32(leftLength));
      }
      else
        {
          rNum = fsRead.Read(buffer, 0, maxLength);
        }
        if (rNum == 0)
        {
          break;
        }
        fileStart += rNum;
        leftLength -= rNum;
        //字節(jié)轉(zhuǎn)換
         string msg = System.Text.Encoding.UTF8.GetString(buffer);//

        byte[] myByte = System.Text.Encoding.UTF8.GetBytes(msg);//
         //寫(xiě)入文件
        using (FileStream fsWrite = new FileStream(@"d:\2.txt, FileMode.Append))//處理完成再追加
        {
          fsWrite.Write(myByte, 0, myByte.Length);
        }
    }
    fsRead.Close();
  }
}

寫(xiě)入文件后期,還牽扯到數(shù)據(jù)的拼接與處理

個(gè)人感覺(jué),數(shù)據(jù)如果要按照一定格式拼接,可以通過(guò)改變每次讀取的位置,來(lái)處理。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論