我如何使用File.ReadAllBytes在块 [英] How do I use File.ReadAllBytes In chunks

查看:1678
本文介绍了我如何使用File.ReadAllBytes在块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code

 字符串LOCATION1 = textBox2.Text;
        字节[]字节= File.ReadAllBytes(LOCATION1);
        字符串文本=(Convert.ToBase64String(字节));
        richTextBox1.Text =文本;

但是当我使用的文件太大,我得到了内存的异常。

我想在块使用 File.ReadAllBytes 。我见过code这样的低于

  System.IO.FileStream FS =新System.IO.FileStream(textBox2.Text,System.IO.FileMode.Open);
字节[] buf中=新的字节[BUF_SIZE]
INT读取动作;//一次读取文件的一种千字节。

{
    读取动作= fs.Read(BUF,0,BUF_SIZE);
    //'BUF'包含读取文件的最后1024字节。}而(读取动作== BUF_SIZE);fs.Close();}

但我不知道如何实际转换读取动作成字节数组,我将转换为文本。

编辑:答案找到。这里是code!

 私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        const int的MAX_BUFFER = 2048;
        字节[]缓冲区=新的字节[MAX_BUFFER]
        INT读取动作;
        使用(System.IO.FileStream FILESTREAM =新的FileStream(textBox2.Text,FileMode.Open,FileAccess.Read))
            而((读取动作= fileStream.Read(缓冲,0,MAX_BUFFER))!= 0)
            {
                字符串文本=(Convert.ToBase64String(缓冲区));
                textBox1.Text =文本;            }}

要改变这是文本格式的读取字节,创建一个新的字节,使其平等
(Convert.FromBase64String(文本))。谢谢大家!


解决方案

 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
    const int的MAX_BUFFER = 2048;
    字节[]缓冲区=新的字节[MAX_BUFFER]
    INT读取动作;
    使用(System.IO.FileStream FILESTREAM =新的FileStream(textBox2.Text,FileMode.Open,FileAccess.Read))
        而((读取动作= fileStream.Read(缓冲,0,MAX_BUFFER))!= 0)
        {
            字符串文本=(Convert.ToBase64String(缓冲区));
            textBox1.Text =文本;        }
}

I am using this code

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

But when I use a file that is too big I get out of memory exception.

I want to use File.ReadAllBytes in chunks. I have seen code like this below

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();

}

But I don't know how to actually convert the bytesRead into a byte array which I will convert to text.

EDIT: Answer found. Here is the code!

 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;

            }

}

To change the readable bytes which are in text format, create a new byte and make it equal (Convert.FromBase64String(Text)). Thanks everyone!

解决方案

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;

        }
}

这篇关于我如何使用File.ReadAllBytes在块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆