2012-07-22 33 views
-1

我使用此代碼如何使用File.ReadAllBytes在塊

 string location1 = textBox2.Text; 
     byte[] bytes = File.ReadAllBytes(location1); 
     string text = (Convert.ToBase64String(bytes)); 
     richTextBox1.Text = text; 

但是當我使用的文件太大,我得到了內存異常。

我想在區塊中使用File.ReadAllBytes。我見過這樣的代碼下面

System.IO.FileStream fs = new System.IO.FileStream(textBox2.Text, System.IO.FileMode.Open); 
byte[] buf = new byte[BUF_SIZE]; 
int bytesRead; 

// Read the file one kilobyte at a time. 
do 
{ 
    bytesRead = fs.Read(buf, 0, BUF_SIZE);    
    // 'buf' contains the last 1024 bytes read of the file. 

} while (bytesRead == BUF_SIZE); 

fs.Close(); 

} 

但我不知道如何實際轉換bytesRead爲字節數組,我將轉換爲文本。

編輯:找到答案。這是代碼!

private void button1_Click(object sender, EventArgs e) 
    { 
     const int MAX_BUFFER = 2048; 
     byte[] Buffer = new byte[MAX_BUFFER]; 
     int BytesRead; 
     using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read)) 
      while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0) 
      { 
       string text = (Convert.ToBase64String(Buffer)); 
       textBox1.Text = text; 

      } 

} 

要更改其是文本格式的可讀字節,創建一個新的字節,並使其等於 (Convert.FromBase64String(文本))。感謝大家!

回答

0
private void button1_Click(object sender, EventArgs e) 
{ 
    const int MAX_BUFFER = 2048; 
    byte[] Buffer = new byte[MAX_BUFFER]; 
    int BytesRead; 
    using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read)) 
     while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0) 
     { 
      string text = (Convert.ToBase64String(Buffer)); 
      textBox1.Text = text; 

     } 
} 
0

不,從Read()返回的值是多少個字節被讀取。數據位於您傳遞給Read()的字節數組buf中。你應該試着理解代碼,而不是複製粘貼&,然後詢問爲什麼它不起作用。即使你這樣做,評論說它在那裏!

+0

我嘗試。你能幫我嗎?如何將所有bufs轉換爲一個大陣列? – Layne3 2012-07-22 02:13:49

+1

你爲什麼要那樣做?這大大破壞了大塊閱讀的全部目的!這正是ReadAllBytes所做的那樣 – 2012-07-22 02:14:43

+0

好吧,我知道它,所以每次它在塊中讀取它,我在哪裏將其轉換爲字符串並將其添加到文本框。像代碼 – Layne3 2012-07-22 02:17:56

0

根據文件結構,使用公開ReadLine方法的StreamReader可能更容易。

using(var sr = new StreamReader(File.Open(textBox2.Text, FileMode.Open)) 
{ 
    while (sr.Peek() >= 0) 
    { 
     Console.WriteLine(sr.ReadLine()); 
    } 
} 
+0

好吧,我怎樣才能將每個塊添加到一個數組中,我將其轉換爲一個字符串。或者將每個字節轉換爲一個字符串並添加到另一個字符串? – Layne3 2012-07-22 02:27:35

+0

只需添加一個字符串......(使用運算符'+') – user844541 2012-07-22 07:11:54

0

如果該文件是一個文本文件,你可以使用的TextReader:

string location1 = textBox2.Text; 
    string text = String.Empty; 
    using (TextReader reader = File.OpenText(location1)) 
    { 
      do 
      { 
      string line = reader.ReadLine(); 
       text+=line; 
      } 
      while(line!=null) 

    } 
1

的bytesRead只是讀取的字節中的計數。

下面是一些塊讀

 var path = @"C:\Temp\file.blob"; 

     using (Stream f = new FileStream(path, FileMode.Open)) 
     { 
      int offset = 0; 
      long len = f.Length; 
      byte[] buffer = new byte[len]; 

      int readLen = 100; // using chunks of 100 for default 

      while (offset != len) 
      { 
       if (offset + readLen > len) 
       { 
        readLen = (int) len - offset; 
       } 
       offset += f.Read(buffer, offset, readLen); 
      } 
     } 

現在,你必須在buffer字節,並可以根據需要將它們轉換。

例如 「使用流」 內:

  string result = string.Empty; 

      foreach (byte b in buffer) 
      { 
       result += Convert.ToChar(b); 
      }