c#常见stream操作

13 篇文章 0 订阅

常见并常用的stream一共有

文件流(FileStream),

内存流(MemoryStream),

压缩流(GZipStream),

加密流(CrypToStream),

网络流(NetworkStream);

1.文件流(读取文件流-输出文件流)FileStream

using(Streamstreamwrite=new FileStream(@"D:\BaiduYunDownload\45.avi",FileMode.OpenOrCreate))
{
using (Stream streamread = new FileStream(@"D:\BaiduYunDownload\xiawu3.avi", FileMode.Open))

byte[] ss=new byte[1024*1024*4];
int len;
while ((len = streamread.Read(ss, 0, ss.Length)) > 0)
{
streamwrite.Write(ss, 0, len);
Thread.Sleep(1000);
}
}
}

2.内存流(MemoryStream)

string strtxt="dasdfdsfsd";
byte[] bytetxt = Encoding.UTF8.GetBytes(strtxt);
Stream memstream = new MemoryStream();
memstream.Write(bytetxt, 0, bytetxt.Length);

3.压缩流(GZipStream),

压缩:

string s = "dfdfdf";
using (FileStream filestream = File.OpenWrite(@"c:\2.txt"))
{
using (GZipStream zipstream = new GZipStream(filestream, CompressionMode.Compress))
{
byte[] bytes = Encoding.UTF8.GetBytes(s);
zipstream.Write(bytes, 0, bytes.Length);
}
}

解压:

using (FileStream filestream = File.OpenRead(@"c:\2.txt"))
{
using (GZipStream zipstream = new GZipStream(filestream, CompressionMode.Decompress))
{
using (FileStream filestreamwrite = new FileStream(@"c:\3.txt", FileMode.OpenOrCreate))
{
byte[] bytes = new byte[1024 * 1024 * 4];
int length;
while ((length = zipstream.Read(bytes, 0, bytes.Length)) > 0)
{
filestreamwrite.Write(bytes, 0, length);
}
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值