我如何将文件复制到字节数组 [英] how i can copy file to byte array

查看:55
本文介绍了我如何将文件复制到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将流文件复制到字节数组
并将字节数组复制到流文件并保存此文件
感谢您的帮助

how i can copy file by stream file to byte array
and copy byte array to stream file and save this file
thanks for any help

推荐答案

将您的文本文件加载到单个字符串中,然后调用此方法:

Load your text file into a single string, and then call this method:

//--------------------------------------------------------------------------------
public static byte[] StringToByteArray(string text)
{
    UTF8Encoding encoding = new UTF8Encoding();
    return encoding.GetBytes(text);
}



要将其放回字符串中,请调用此方法:



To put it back into a string, call this method:

//--------------------------------------------------------------------------------
public static string ByteArrayToString(byte[] array)
{
    UTF8Encoding encoding = new UTF8Encoding();
    string text  = encoding.GetString(array);
    return text;
}


public bool writeByteArrayToFile(byte[] buff, string fileName)
{
bool response = false;

try
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff);
bw.Close(); //Thanks Karlo for pointing out!
response = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

return response;
}


有关保存文件,请参见此链接
点击


For save file, see this link
Click


这篇关于我如何将文件复制到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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