2013-04-16 107 views
0

我一直在嘗試幾天來以塊加載文件,以允許用戶使用非常大的(GB)文件並仍然保持程序的速度。目前,我有以下代碼:將文件加載/流式傳輸到緩衝區/緩衝區

using (FileStream filereader = new FileStream(filename, FileMode.Open, FileAccess.Read)) 
    { 
    using (StreamReader reader = new StreamReader(filereader)) 
    { 
     while (toRead > 0 && (bytesread = reader.Read(buffer, offset, toRead)) > 0) 
     { 
     toRead -= bytesread; 
     offset += bytesread; 
     } 

     if (toRead > 0) throw new EndOfStreamException(); 

     foreach (var item in buffer) 
     { 
     temporary = temporary += item.ToString(); 
     } 
     temporary.Replace("\n", "\n" + System.Environment.NewLine); 

下面是避免任何混淆的聲明(希望):

const int Max_Buffer = 5000; 
    char[] buffer = new char[Max_Buffer]; 
    int bytesread; 
    int toRead = 5000; 
    int offset = 0; 

目前該計劃在5000個字節的文本文件的讀取,然後處理字節轉換成一個字符串,然後傳入一個字符串讀取器,這樣我就可以獲取我想要的信息。

我現在的問題是緩衝區可以在一行中途停下來,所以當我在stringreader類中取得數據時,它會引起索引/長度錯誤。

我需要的是要知道如何在數組中尋找一組表示行的開始的特定字符集,然後僅在該點之前返回數據以處理爲字符串。

排序回溯問題後的另一個問題是我將如何保持我不想處理的數據並引入更多數據來填充緩衝區。

我希望這個解釋得很好,我知道我有時會困惑,希望有人能幫助。

+1

在每次換行時都打破頂端?與使用ReadLine()相同然後你只需要向前查找。您可以使用字符串生成器來存儲任何臨時行(通過Max_buffer的行) –

回答

0

我會建議使用reader.ReadLine()的代替reader.Read()在你的循環

buffer=reader.ReadLine(); 
bytesread = buffer.Length*2;//Each charcter is unicode and equal to 2 bytes 

您可以然後檢查是否(探路者 - bytesread)< 0 。