C#中流的使用和分類
使用流讀取、寫入文件
使用流把文件讀取到字節(jié)數(shù)組:
//FileMode.Create, FileMode.Append //FileAccess.Write, FileAccess.ReadWrite //FileMode和FileAccess搭配使用,如果第二個(gè)參數(shù)FileMode.Appden寫追加,第三個(gè)參數(shù)FileAccess.Read只讀,會(huì)拋異常 Stream source = new FileStream(@"1.jpg",FileMode.Open, FileAccess.Read) byte[] buffer = new byte[source.Length]; int bytesRead = source.Read(buffer, i, (int)source.Length);
Int32類型的最大值,以及Byte, KB, MB, GB轉(zhuǎn)換:
Int32.MaxValue = 2147483647 Byte
2147483647/1024 = 2097152 KB(1 KB = 1024 Byte)
2097152/1024 = 2048 MB(1 M = 1024 KB)
2048/1024 = 2 G(1G = 1024M)
使用流把字節(jié)數(shù)組寫到文件:
Stream target = new FileStream(@"2.jpg", FileMode.Create, FileAccess.Write); Stream source = new FileStream(@"1.jpg",FileMode.Open, FileAccess.Read) byte[] buffer = new byte[source.Length]; int bytesRead = source.Read(buffer, i, (int)source.Length); target.Write(buffer, 0, buffer.Length); source.Dispose(); target.Dispose();
使用流對(duì)大文件進(jìn)行分批讀取和寫入:
int BufferSize = 10240; // 10KB Stream source = new FileStream(@"D:\a.mp3", FileMode.Open, FileAccess.Read); Stream target = new FileStream(@"D:\b.mp3", FileMode.Create, FileAccess.Write); byte[] buffer = new byte[BufferSize]; int byteRead; do{ byteRead = source.Read(buffer, 0, BufferSize); target.Write(buffer, 0, bytesRead); } while(byteRead > 0); target.Dispose(); source.Dispose();
流的分類
在Stream抽象類下包含:
→FileStream→IsolatedStoreageFileStream
→MemoryStream
→NetworkStream
基礎(chǔ)流
從流中讀取數(shù)據(jù):
CanRead()
Read(byte[] buffer, int offset, int count)
向流中寫入數(shù)據(jù):
CanWrite()
Write(byte[] buffer, int offset, int count)
WriteByte(Byte value)
移動(dòng)流指針:
CanSeek()
Seek(long offset, SeekOrigion)
Position流的指針位置
Close()
Dispose()
Flush()將緩存設(shè)備寫入存儲(chǔ)設(shè)備
CanTimeout()
ReadTimeout()
WriteTimeout()
Length
SetLength(long value)
裝飾器流
實(shí)現(xiàn)了Decorator模式,包含對(duì)Stream抽象基類的引用,同時(shí)繼承自Stream抽象基類。
- System.IO.Compression下的DeflateStream和GZipStream用于壓縮和解壓縮
- System.Security.Cryptography下的CryptoStream用于加密和解密
- System.Net.Security下的AuthenticatedStream用于安全性
- System.IO下的BufferedStream用戶緩存
包裝器類
不是流類型,而是協(xié)助開發(fā)者處理流包含的數(shù)據(jù),并且不需要將流讀取到Byte[]字節(jié)數(shù)組中。但流的包裝器類包含了對(duì)流的引用。
StreamReader
繼承自TextReader。
將流中的數(shù)據(jù)讀取為字符。
FileStream fs = new FileStream("a.txt", FileMode.Open, FileAcess.Read); StreamReader reader = new StreamReader(fs, Encoding.GetEncoding("GB2312")); //或者 //StreamReader reader = new StreamReader("a.txt"); //默認(rèn)采用UTF-8編碼方式
StreamWriter
繼承自TextWriter。
將字符寫入到流中。
string text = @"aa bb cc"; StringReader reader = new StringReader(text); int c = reader.Read(); Console.Write((char)c); char[] buffer = new char[8]; reader.Read(buffer, 0, buffer.Length); Console.Write(String.Join("",buffer)); string line = reader.ReadLine(); Console.WriteLine(line); string rest = reader.ReadToEnd(); Console.Write(); reader.Dispose();
StringReader和StringWriter
也繼承自TextReader和TextWriter,但是用來處理字符串。
BinaryWriter和BinaryReader
BinaryWriter用于向流中以二進(jìn)制方式寫入基元類型,比如int, float, char, string等.BinaryReader用于從流中讀取基元類型。注意,這2個(gè)類并不是繼承TextReader和TextWriter。
namespace ConsoleApplication29 { class Program { static void Main(string[] args) { Product p = new Product("product.bin") { Id = 1, Name = "GOOD", Price = 500F }; p.Save(); Product newP = new Product("product.bin"); newP.Load(); Console.WriteLine(newP); Console.ReadKey(); } } 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); } } }
結(jié)果:
編碼方式:
定義了字節(jié)如何轉(zhuǎn)換成人類可讀的字符或者文本,可以看作是字節(jié)和字符的對(duì)應(yīng)關(guān)系表。在讀取文件采用的編碼方式要和創(chuàng)建文件采用的編碼方式保持一致。
幫助類
在System.IO命名空間下。
- File
FileStream fs = File.Create("a.txt");
Open(string path, FileMode mode)
OpenRead()
OpenWrite()
ReadAllText()
ReadAllByte()
WriteBllBytes()
WriteAllLines()
Copy(string sourceFileName, string destFileName)
- FileInfo
- Path
- Directory
- DirectoryInfo
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C#實(shí)現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法
這篇文章主要介紹了C#實(shí)現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法,涉及C#數(shù)值判定與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06Unity ScrollView實(shí)現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Unity ScrollView實(shí)現(xiàn)無限循環(huán)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片
這篇文章主要介紹了c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片的方法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2021-01-01C#使用SqlDataAdapter對(duì)象獲取數(shù)據(jù)的方法
這篇文章主要介紹了C#使用SqlDataAdapter對(duì)象獲取數(shù)據(jù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了SqlDataAdapter對(duì)象獲取數(shù)據(jù)具體步驟與相關(guān)使用技巧,需要的朋友可以參考下2016-02-02C#?漢字與拼音互轉(zhuǎn)的實(shí)現(xiàn)示例
本文主要介紹了C#?漢字與拼音互轉(zhuǎn)的實(shí)現(xiàn)示例,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03探討:關(guān)閉瀏覽器后,php腳本會(huì)不會(huì)繼續(xù)運(yùn)行
本篇文章是對(duì)關(guān)閉瀏覽器后,php腳本會(huì)不會(huì)繼續(xù)運(yùn)行進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06