首页 > 代码库 > c#常见stream操作
c#常见stream操作
常见并常用的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);
}
}
}
}