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.");
        }

  • 相关阅读:
    思维导图
    第九周学习
    java连接数据库执行SQL并把查询到的数据保存到磁盘
    火狐浏览器安装firebug和firepath插件方法(离线)
    selenium自动化测试资源整理(含所有版本chrome、chromedriver、firefox下载链接)
    Jenkins调度Selenium脚本不能打开浏览器解决办法
    selenium如何操作HTML5的画布canvas上的元素
    Java中如何使用非强制类型转换把字符串转换成int类型
    自动化测试如何使用driver.findElements去操作页面元素
    Jmeter响应数据中文乱码
  • 原文地址:https://www.cnblogs.com/chenli0513/p/1871136.html
Copyright © 2011-2022 走看看