//stream 转为byte[]
public byte[] stream2byte(Stream stream)
{
byte[] buffer= new byte[stream.length];
stream.Read(buffer,0,buffer.length);
return buffer;
}
//byte[] 转stream
public stream byte2stream(byte[] buffer)
{
stream stream= new MemoryStream(buffer);
stream.Seek(0,SeekOrigin.Begin);
//设置stream的position为流的开始
return stream;
}
//stream写到文件
public void stream2File(Stream stream)
{
byte[] buffer= new buffer[stream.length];
stream.Read(buffer,0,buffer.length);
FileStream fs= new FileStream(filename,FileMode.Create);
BinaryWriter writer= new BinaryWriter(fs);
writer.Writer(buffer);
writer.Close();
fs.Close();
}
//读取文件到stream
public Stream file2Stream(string path)
{
FileStream fs = new FileStream(path,fileMode.Open,FileAccess.Read);
byte[] buffer= new byte[fs.length];
fs.Read(buffer,0,buffer.length);
Stream stream=MemoryStream(buffer);
stream.Seek(0,seekOrigin.Begin);
return stream;
}
stream 与byte[] 转换
最新推荐文章于 2024-06-11 15:49:24 发布