zoukankan      html  css  js  c++  java
  • C#学习之Stream

    上节课老师主要讲了stream及其一些运用和实例,下面我们就来讨论讨论Stream。

    我们知道,C#中常用的stream有文件流(FileSteam),内存流(MemoryStream),压缩流(GZipStream),加密流(CrypToStream),网络流(NetworkStream)。

    下面是一些代码的展示:

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

    using(Streamstreamwrite=new FileStream(@"D:BaiduYunDownload45.avi",FileMode.OpenOrCreate))

    {

    using (Stream streamread = new FileStream(@"D:BaiduYunDownloadxiawu3.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);

    }

    }

    }

    }

  • 相关阅读:
    CCOrbitCamera卡牌翻转效果
    用CCRenderTexture和BlendFunc制作游戏教学时使用的黑色覆盖层
    cocos2d-x触摸分发器原理
    cocos2d-x动作原理
    c++之函数原型
    cocos2d-x调度器原理
    cocos2d-x之MoonWarriors用c++实现
    cocos2d-x回收池原理
    SQL*Net more data to client等待事件
    asynch descriptor resize等待事件
  • 原文地址:https://www.cnblogs.com/lixuntaohaolihai/p/4495169.html
Copyright © 2011-2022 走看看