• 技术文章 >后端开发 >C#.Net教程

    .net 流——流的类型体系简单介绍

    黄舟黄舟2017-02-24 10:42:38原创825

    流的类型体系

    基础流

    装饰器流

    包装器类

    帮助类

    继承自基本的stream

    在基础Stream上添加的功能

    数据传输

    对文件流的操作变简单


    基础流

    Stream

    对应的后备存储是文件

    内存

    网络资源

    FileStream

    MemoryStream

    NetWorkStream

    IsolatedStorgaeFileStream:

    继承自FileStream

    感觉还是挺类Java的。


    装饰器流

    BufferdStream

    DeflateStream

    GZipStream

    CryptoStream

    AuthenticatedStream

    System.io

    System.IO.Compression

    System.IO.Compression

    System.Security.Cryptography

    System.net.security

    增强缓存

    压缩

    解压缩

    加密解密

    安全性

    联想装饰者模式,估计是在基本流上面加上一些其他功能,有点儿像对基本类型的功能上的扩展。

    包装器类

    介绍流的用于数据传输;

    TextWriter

    TextReader(定义的一组通用的,读取和写入字符数据的方式)

    StreamReader

    StreamWriter

    1,继承自

    TextWriter

    TextReader,定义了一组通用的,读取和写入字符数据的方式

    2,适用于读取和写入文本字符的场合

    BinaryReader:用于向流中以二进制的方式写入基元类型

    BinaryWriter:用于从流中读取基元类型

    StringReader

    StringWriter:

    1,也继承自

    TextWriter

    TextReader,用于处理字符串

     #region StringReader,StringWriter的使用;
    
    //                string text = @"姓名:水田如雅;
    //                                年龄:20;
    //                                性别:女";
    //                StringReader reader = new StringReader(text);
    //                int c = reader.Read();
    //                Console.WriteLine((char)c);
    
    //                char[] buffer = new char[7];
    //                reader.Read(buffer, 0, buffer.Length);
    //                Console.WriteLine(string.Join("", buffer));
    
    //                string line = reader.ReadLine();
    //                Console.Write(line);
    
    //                string rest = reader.ReadToEnd();
    //                Console.WriteLine(rest);
    
    //                reader.Dispose();
    //                Console.ReadKey();
    
                #endregion
    
                #region streamreader/StreamWriter的使用
    
                    //FileStream fs = new FileStream("AboutVac.txt", FileMode.Open, FileAccess.Read);
                    //StreamReader reader=new StreamReader(fs,Encoding.GetEncoding("GB2312"));
    
                    //do
                    //{
                    //    Console.WriteLine(reader.ReadLine());
    
                    //} while (reader.Read()>0);
    
                    //StreamWriter writer = new StreamWriter("aboutMe.txt", false, Encoding.UTF8);
                    //writer.Write("hello,word");
                  
                    //reader.Dispose();
                    //writer.Dispose();
    
                    //Console.ReadKey();
    
    
                #endregion
    
                #region BinaryReader/BinaryWriter的使用
    
                    //Product product = new Product("Product.txt") { 
                
                    //        id=110,
                    //        price=123.3,
                    //        Name="lhc"
                
                    //};
                    //product.Save();
                    //Product newItem =new  Product("Product.txt");
                    //newItem.Load();
                    //Console.WriteLine(newItem);
                    //Console.ReadKey();
                    
    
    
                #endregion
    public class Product {
    
            public int id { get; set; }
            public string Name { get; set; }
            public double price { get; set; }
    
            private string filePath;
            public Product(string filePath) { this.filePath = filePath; }
    
            public void Save() {
    
                FileStream fs = new FileStream(this.filePath, FileMode.Create, FileAccess.Write);
                BinaryWriter writer = new BinaryWriter(fs);
                writer.Write(this.id);
                writer.Write(this.Name);
                writer.Write(this.price);
                writer.Dispose();
            }
    
            public void Load() {
    
                FileStream fs = new FileStream(this.filePath, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                this.id = reader.ReadInt32();
                this.Name = reader.ReadString();
                this.price = reader.ReadDouble();
                reader.Dispose();
            
            }
    
            public override string ToString()
            {
                return string.Format("ID:{0},Name:{1},Price:{2}", this.id, this.Name, this.price);
    
            }
        }

    帮助类

    命名空间:system.io

    File

    FileInfo

    Path

    Directory/DirecoryInfo

    Create;

    Open;

    openRead;

    openWrite;

    Copy;

    处理路径




      #region 工具类示例
                   // File.WriteAllText("FileTest.txt", "hello,i'm using file ", Encoding.UTF8);
      #endregion

    在使用的时候,还是应该先考虑已有的高级类是否封装了可用的方法,没有的话,再考虑使用基本类进行封装。


    以上就是.net 流——流的类型体系简单介绍的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!









    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:.Net——使用DataContractJsonSerializer进行序列化及反序列化基本操作 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 【c#教程】C# 属性(Property)• ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 应用绝对路径与相对路径• C#中GDI+编程10个基本技巧二• C/C++深度分析
    1/1

    PHP中文网