zoukankan      html  css  js  c++  java
  • .Net之美读书笔记8

    流与序列化

    流的定义

    流的用途是与应用程序外部的文件或数据进行数据交互。比如在访问文件时,有文件流(FileStream);在访问网络时,有网络流(NetworkStream)等。
    这些流都继承自基类(Stream),因此他们的使用方式基本一致。
    操作流的demo

            int bufferSize = 10240;
    
                using (Stream source = new FileStream(@"H:DotNetBeautitygoodMan.mp3", FileMode.Open, FileAccess.Read),
                    target = new FileStream(@"H:GoodMam.mp3", FileMode.Create, FileAccess.Write))
                {
                    byte[] buffer = new byte[bufferSize];
                    int bytesRead;
                    do
                    {
                        long i = source.Position;
                        //第一个参数为字节数组,相对与第一个的偏移量,读取大小
                        bytesRead = source.Read(buffer, 0, bufferSize);
                        target.Write(buffer, 0, bytesRead);
                    } while (bytesRead > 0);
                }
    

    流的知识体系

    1. 基础流
    • FileStream 文件流
    • MemoryStream 后背存储是内存,提供一种流的方式处理byte[]
    • NetworkStream 网络流
    1. 装饰流,装饰流实现了Decorator模式,为某些流添加了功能。使用时将流传入对应装饰流的构造函数,调用装饰流的实体的方法。
    • BufferedStream 缓存处理,流读取大文件式,能提升速度
    • DeflateStream 、GZipStream压缩解压流
    • CryptoStream用于加密解密
    • AuthenticatedStream 用于安全
    1. 包装器类,对流的读入和写入提供方便,成对出现处理读取、写入。注意关于字符需要读取写入编码一致。
    • StreamReader、StreamWriter 以字符的形式读取写入流
    • StringReader、StringWriter
    1. 帮助类,主要涉及 File、FileInfo等处理流的方法

    系列化

    系列化:将内存中的对象及其状态保存起来。
    反序列化: 将文件还原为内存中的对象

    系列化的两个类

    • BinaryFormatter 序列化为二进制
    • SoapFormatter 序列化为xml;(SOAP Simple Object Access Protocol)简单对象访问协议
        [Serializable]
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public double Price { get; set; }
            public override string ToString()
            {
                return string.Format("ID:{0},Name:{1},Price:{2}",this.Id,this.Name,this.Price);
            }
        }
        //调用
            IFormatter formatter = new BinaryFormatter();
            //序列化
            Product item = new Product() { Id = 1, Name = "Apple X", Price = 8388 };
            using(Stream fs = File.Create(@"H:product.obj"))
            {
                    formatter.Serialize(fs, item);
            }
            //反序列化
            Product itemAnother;
            using (Stream fs = File.OpenRead(@"H:product.obj"))
            {
                itemAnother = (Product)formatter.Deserialize(fs);
            }
            Console.WriteLine(itemAnother.ToString());
    

    这里注意如果某个属性或字段不需要系列化[NonSerialized]特性标记,还有序列化过和反省序列化中存在事件, [OnSerializing] [OnSerialized] [OnDeserializing][OnDeserialized]使用方法

        [OnSerializing]
        void OnSerializing(StreamingContext context)
        {
    
        }
    
  • 相关阅读:
    定义字符串数组
    ifconfig 修改IP
    空指针与野指针的区别
    GDB和Core Dump使用笔记
    雅虎(ycsb)测试hbase(压测)
    decode函数的几种用法
    NVL函数:空值转换函数
    hive行转列,列转行
    case when then else end用法
    hive中一般取top n时,row_number(),rank,dense_ran()常用三个函数
  • 原文地址:https://www.cnblogs.com/LoveTomato/p/8047409.html
Copyright © 2011-2022 走看看