2012-07-22 84 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); 
      }