且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-08-14 14:53:53


 

流的类型体系

 

基础流

装饰器流

包装器类

帮助类

继承自基本的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


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