zoukankan      html  css  js  c++  java
  • C# 实践 7 MemoryStream Class

    参考:

    https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream?view=netframework-4.8

    “创建一个流,其后备存储为内存。”

    参考代:

    using System;
    using System.IO;
    using System.Text;
    
    class MemStream
    {
        static void Main()
        {
            int count;
            byte[] byteArray;
            char[] charArray;
            UnicodeEncoding uniEncoding = new UnicodeEncoding();
    
            // Create the data to write to the stream.
            byte[] firstString = uniEncoding.GetBytes(
                "Invalid file path characters are: ");
            byte[] secondString = uniEncoding.GetBytes(
                Path.GetInvalidPathChars());
    
            using(MemoryStream memStream = new MemoryStream(100))
            {
                // Write the first string to the stream.
                memStream.Write(firstString, 0 , firstString.Length);
    
                // Write the second string to the stream, byte by byte.
                count = 0;
                while(count < secondString.Length)
                {
                    memStream.WriteByte(secondString[count++]);
                }
    
                // Write the stream properties to the console.
                Console.WriteLine(
                    "Capacity = {0}, Length = {1}, Position = {2}
    ",
                    memStream.Capacity.ToString(),
                    memStream.Length.ToString(),
                    memStream.Position.ToString());
    
                // Set the position to the beginning of the stream.
                memStream.Seek(0, SeekOrigin.Begin);
    
                // Read the first 20 bytes from the stream.
                byteArray = new byte[memStream.Length];
                count = memStream.Read(byteArray, 0, 20);
    
                // Read the remaining bytes, byte by byte.
                while(count < memStream.Length)
                {
                    byteArray[count++] =
                        Convert.ToByte(memStream.ReadByte());
                }
    
                // Decode the byte array into a char array
                // and write it to the console.
                charArray = new char[uniEncoding.GetCharCount(
                    byteArray, 0, count)];
                uniEncoding.GetDecoder().GetChars(
                    byteArray, 0, count, charArray, 0);
                Console.WriteLine(charArray);
            }
        }
    }
    

      

  • 相关阅读:
    修改Matlab 2012b默认工作路径
    win7,M​i​n​d​m​a​n​a​g​e​r​2​0​1​2使用模板时弹出Runtime error R6025解决方法
    Win7 Qt4.8.5+QtCreator2.8.0+mingw配置过程
    RabbitMQ介绍和延迟队列
    FastDFS介绍
    SpringMVC的工作流程
    spring mvc入参有Date类型
    spring 定时任务的 时间配置cron表达式
    Transactional介绍及使用
    spring cloud 集成rabbitMQ实现延时队列
  • 原文地址:https://www.cnblogs.com/alexYuin/p/12404437.html
Copyright © 2011-2022 走看看