C#Byte[]数组读取和写⼊⽂件
protected void ByteToString_Click(object sender, EventArgs e)
{
string content = Content.Text.ToString();
if (string.IsNullOrEmpty(content))
{
return;
}
//string 转为byte数组
byte[] array = Encoding.UTF8.GetBytes(content);
//将byte数组转为string
string result = Encoding.UTF8.GetString(array);
字符串数组怎么转成byteResponse.Write(result);
}
//利⽤byte[]数组写⼊⽂件
protected void writerFile_Click(object sender, EventArgs e)
{
string content = Content.Text.ToString();
if (string.IsNullOrEmpty(content))
{
return;
}
//将string转为byte数组
byte[] array = Encoding.UTF8.GetBytes(content);
string path = Server.MapPath("/");
//创建⼀个⽂件流
FileStream fs = new FileStream(path, FileMode.Create);
//将byte数组写⼊⽂件中
fs.Write(array, 0, array.Length);
//所有流类型都要关闭流,否则会出现内存泄露问题
fs.Close();
Response.Write("保存⽂件成功");
}
//利⽤byte[]数组读取⽂件
protected void readFile_Click(object sender, EventArgs e)        {
string path = Server.MapPath("/");
FileStream fs = new FileStream(path, FileMode.Open);
//获取⽂件⼤⼩
long size = fs.Length;
byte[] array = new byte[size];
//将⽂件读到byte数组中
fs.Read(array, 0, array.Length);
fs.Close();
//将byte数组转为string
string result = Encoding.UTF8.GetString(array);
Response.Write(result);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。