C# FileStream簡單介紹和使用
本章講述:FileStream類的基本功能,以及簡單示例;
1、引用命名空間:using System.IO;
2、注意:使用IO操作文件時,要注意流關閉和釋放問題!
強力推薦:將創(chuàng)建文件流對象的過程寫在using當中,會自動幫助我們釋放資源;
使用try{} catch(Exception ex){} 進行一次捕獲;
3、FileStream 操作字節(jié),可以操作任何類型的文件;下面來簡單介紹FileStream類的方法和參數(shù):
(1)FileStream() 作用:創(chuàng)建FileStream對象,參數(shù):第一個是路徑,第二個是文件模式FileMode枚舉,第三個數(shù)據模式FileAcess
FileStream(String, FileMode): FileStream(String, FileMode, FileAccess) FileStream(String, FileMode, FileAccess, FileShare) FileStream(String, FileMode, FileAccess, FileShare, Int32)
初始化FileStream時使用包含文件共享屬性(System.IO.FileShare)的構造函數(shù)比使用自定義線程鎖更為安全和高效
(2)FileMode(以何種方式打開或者創(chuàng)建文件):CreateNew(創(chuàng)建新文件)、Create(創(chuàng)建并覆蓋)、Open(打開)、OpenOrCreate(打開并創(chuàng)建)、Truncate(覆蓋文件)、Append(追加);
(3)FileAcess(文件流對象如何訪問該文件):Read(只讀) 、Write(寫)、ReadWirte(讀寫);
(4)FileShare(進程如何共享文件):None(拒絕共享)、Read 、Write、ReadWrite(同時讀寫)、Delete;
(5)bufferSize(緩沖區(qū)大小設置)
4、Stream.Read(array<Byte[], Int32, Int32):從流中讀取一塊字節(jié),并將數(shù)據寫入給定的緩沖區(qū);
5、Stream.Write(array<Byte[], Int32, Int32):使用緩沖區(qū)中的數(shù)據將字節(jié)塊寫入此流;
6、close():關閉當前流并釋放與當前流關聯(lián)的任何資源(如套接字和文件句柄);
7、dispose():釋放流所有使用的資源;
8、CopyTo(Stream):從當前流中讀取所有字節(jié)并將其寫入目標流。
CopyTo(Stream, Int32):從當前流中讀取所有字節(jié),并使用指定的緩沖區(qū)大小將它們寫入目標流
9、Seek()(FileStream類維護內部文件指針,該指針指向文件中進行下一次讀寫操作的位置):將此流的當前位置設置為給定值。(stream.seek(Int64,SeekOrigin)
第一個參數(shù)規(guī)定文件指針以字節(jié)為單位的移動距離。第二個參數(shù)規(guī)定開始計算的起始位置;SeekOrigin枚舉包含3個值:Begin、Current 和 End;
例如:aFile.Seek(0, SeekOrigin.End);
10、由于設置了文件共享模式為允許隨后寫入,所以即使多個線程同時寫入文件,也會等待之前的線程寫入結束之后再執(zhí)行,而不會出現(xiàn)錯誤
11、簡單示例1:簡單文件寫入
FileStream devStream = new FileStream(devPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite,512); devStream.Write(data, 0, 128); if(devStream != null) devStream.Close();
12、簡單示例2:以追加的方式寫入文件
public static class MonitData { public static string devPath = string.Empty; private static object objLock = new object(); public static void WriteInfo(byte[] data) { lock (objLock) { if (!string.IsNullOrEmpty(devPath)) { byte[] byteArray = new byte[128]; Array.Copy(data, 0, byteArray, 0, 128); if (byteArray != null && byteArray.Length == 128) { using (System.IO.FileStream fs = System.IO.File.OpenWrite(devPath)) { fs.Seek(0, SeekOrigin.End); fs.Write(byteArray, 0, byteArray.Length); fs.Close(); fs.Dispose(); } } } } } }
13、簡單示例:文件流寫入
public static void Main(string[] args) { String str = @"E:\下載\軟件"; Stopwatch sw = new Stopwatch(); sw.Start(); using (FileStream fsWriter = new FileStream(str + @"\opencv-3.0.exe", FileMode.Create, FileAccess.Write)) { using (FileStream fsReader = new FileStream(str + @"\opencv-2.4.9.exe", FileMode.Open, FileAccess.Read)) { byte[] bytes=new byte[1024*4];//4kB是合適的; int readNum; while((readNum=fsReader.Read(bytes,0,bytes.Length))!=0)//小于說明讀完了 { fsWriter.Write(bytes,0,readNum); fsWriter .Flush();//清除緩沖區(qū),把所有數(shù)據寫入文件中 fsWriter.Close(); fsWriter.Dispose(); } } } sw.Stop(); Console.WriteLine("總的運行時間為{0}",sw.ElapsedMilliseconds); Console.ReadKey(); }
14、簡單示例:讀取文件
public static string FileStreamReadFile(string filePath) { byte[] data = new byte[100]; char[] charData = new char[100]; FileStream file = new FileStream(filePath, FileMode.Open); //文件指針指向0位置 file.Seek(0, SeekOrigin.Begin);//可以設置第一個參數(shù) //讀入兩百個字節(jié) file.Read(data, 0, (int) file.Length); //提取字節(jié)數(shù)組 Decoder dec = Encoding.UTF8.GetDecoder(); dec.GetChars(data, 0, data.Length, charData, 0); file.Close(); file.Dispose(); return Convert.ToString(charData); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#使用WebSocket與網頁實時通信的實現(xiàn)示例
本文主要介紹了C#使用WebSocket與網頁實時通信的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn)
本文主要介紹了C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn),它返回當前應用程序的工作目錄路徑,具有一定的參考價值,感興趣的可以了解一下2024-02-02C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類介紹
本文主要介紹了C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類的基礎的使用,并做了相關的代碼演示,供初學者參考。2016-03-03