当前位置:   article > 正文

Stream流_using stream stream = new stream(stream)

using stream stream = new stream(stream)

流是C#中比较重要的一个概念,很多关键技术都需要用到流。何为流呢?可以理解流为江河中水的流动,不过C#中则为信息流,我们可以把信息写入流,也可以读出。比如以文件读写操作为例,首先以某种方式(如只读)打开文件,相当于建立了通往此文件的流,然后就可以从此流中读取信息。C# stream为抽象类,只能用来定义。由它派生出三个派生类:MemoryStream(内存流),BufferedStream(缓存流)以及filestream(文件流)。以下例子由http://www.2cto.com/kf/201203/123535.html转载而来,并且此例子有更加详细的说明。本例子由memorystream进行构造,此类进行数据读取格式为byte类型,中间需要用到相关转化(encoding类),其实流的操作均为byte类型,采用不同的reader,可以简化操作,此例子以memorystream的write,read方法来进行相关操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace StreamStu
{
    class Program
    {
        static void Main(string[] args)
        {
            //本节主讲stream的用法
            //通过二进制把,数据读入流,并读出流
            string testStr = "Stream!Hello world";//读入的数据(字符串)
            byte[] buffer = null;//中间数据传输用的缓冲字节数组
            byte[] readBuffer = null;//读取数据的缓冲字节数组
            char[] readArr;//解码出char的缓冲字符数组
            string stringArr = string.Empty;//存放解码出的string

            //开始文件的读入;
            Console.WriteLine("初始字符串为:" + testStr);
            using (Stream stream = new MemoryStream())
            {
                if (stream.CanWrite)//判断当前流是否可写入
                {
                    //数据写入
                    buffer = Encoding.Default.GetBytes(testStr);
                    stream.Write(buffer, 0, 3);//从stream.position=0的位置读入三个值,新的位置位于3+1的位置;
                    long newStreamPos = stream.CanSeek ? stream.Seek(3, SeekOrigin.Current) : 0;//读取位置从当前为位置偏移3;
                    if ((int)newStreamPos < buffer.Length)//判断移动位置是否超出总长度;
                    {
                        stream.Write(buffer, (int)newStreamPos, buffer.Length - (int)newStreamPos);
                    }
                }

                stream.Position = 0;//读取时,初始位置设为0
                readBuffer = new byte[buffer.Length];
                int count = stream.CanRead ? stream.Read(readBuffer, 0, buffer.Length) : 0;//从流中读入readbuffer,read返回值为读入的个数,write无返回值

                //将readbuffer转换为string
                int charCount = Encoding.Default.GetCharCount(readBuffer, 0, count);//确定解码的字符个数,即readbuffer中,从0开始,解码count个
                readArr = new char[charCount];
                Encoding.Default.GetDecoder().GetChars(readBuffer, 0, count, readArr, 0);//从readbuffer中0位置开始解码,解码count个到readarr,并从readarr的0位置开始写入
                for (int i = 0; i < readArr.Length; i++)
                {
                    stringArr += readArr[i];
                }
                stream.Close();
            }
            Console.WriteLine(stringArr);
            
            Console.ReadKey();

        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

其实进行数据读取时,可以用streamReader,可以省略将byte数据转化成char[]进而转化成string的操作,代码如下:

                //用streamreader方法数据读取
                //stream.Position = 0;//读取时,初始位置设为0
                //StreamReader sr = new StreamReader(stream);
                //string str = sr.ReadToEnd();
                //Console.WriteLine(str);
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/367357
推荐阅读
相关标签
  

闽ICP备14008679号