zoukankan      html  css  js  c++  java
  • BinaryWriter 及BinaryReader 在二进制的读与写,做好笔记,以备忘。

                                                                                                                                                         

          

    BinaryWriter  BinaryReader 是一个二进制流管理对象,方便我们操作这些二进制数据,值得借鉴,相比我们自己去拼接与遍历要好的多。

        

    代码
    static void Main()
        {
            
    const int arrayLength = 1000;

            
    // Create random data to write to the stream.
            byte[] dataArray = new byte[arrayLength];
            
    new Random().NextBytes(dataArray);

            BinaryWriter binWriter 
    = new BinaryWriter(new MemoryStream());

            
    // Write the data to the stream.
            Console.WriteLine("Writing the data.");
            binWriter.Write(dataArray);

            
    // Create the reader using the stream from the writer.
            BinaryReader binReader = 
                
    new BinaryReader(binWriter.BaseStream);

            
    // Set Position to the beginning of the stream. //设置流开始的位置
            binReader.BaseStream.Position = 0;

            
    // Read and verify the data. 读取并验证数据,从二进制流中读取。  
            byte[] verifyArray = binReader.ReadBytes(arrayLength);  //从binReader对象当中读取指定长度数据 binReader对象已经被初始化。
            if(verifyArray.Length != arrayLength)
            {
                Console.WriteLine(
    "Error writing the data.");
                
    return;
            }
            
    for(int i = 0; i < arrayLength; i++)
            {
                
    if(verifyArray[i] != dataArray[i])
                {
                    Console.WriteLine(
    "Error writing the data.");
                    
    return;
                }
            }
            Console.WriteLine(
    "The data was written and verified.");
        }

  • 相关阅读:
    字符串的操作
    10.20 整理1
    if else; while; break;continue ----流程控制系列
    10.19 重新打了第一天的代码(课件))
    10.17
    svn,导入数据到版本库及使用工作副本
    mysql的反引号backtick
    css中的1px并不等于设备的1px
    onclick事件属性与在用js绑定onclick事件中的this的区别
    一次对CI框架update方法底层实现的探索之旅
  • 原文地址:https://www.cnblogs.com/chenli0513/p/1871136.html
Copyright © 2011-2022 走看看