.net 流-流的類型體系簡單介紹

黄舟
發布: 2017-02-24 10:42:38
原創
1509 人瀏覽過


流的型別系統

 

# 基礎流裝飾器流

##幫助類別

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

增強快取

########################### ######壓縮############解壓縮#############加密解密############安全性# ##############

 

 聯想裝飾者模式,估計是在基本流上面加上一些其他功能,有點兒像對基本類型的功能上的擴展。

 

包裝器類別

 

介紹流的用於資料傳輸;

 

 

 

 #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

#TextWriter

#Text#Reader(定義的一組通用的,讀取和寫入字元資料的方式)

 

StreamReader

StreamWriter

1,繼承自

TextWriter

#TextReader,定義了一組通用的,讀取和寫入字元資料的方式

 

2,適用於讀取和寫入文字字元的場合

 

#BinaryReader##:用於向流中以二進位的方式寫入基底元類型

BinaryWriter#:用於從流中讀取基元類型

StringReader

StringWriter:

1,也繼承自

##TextWriter

Text

Reader,用於處理字串

 

##FileInfoPath##Create;
#File

Directory

/DirecoryInfo

Open;

openRead;

openWrite;

Copy;

 

處理路徑






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


在使用的時候,還是應該先考慮已有的高階類別是否封裝了可用的方法,沒有的話,再考慮使用基本類別進行封裝。



 以上就是.net 串流-串流的型別系統簡單介紹的內容,更多相關內容請關注PHP中文網(www.php .cn)!









###########################################################
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!