本章講述:FileStream類的基本功能,以及簡單示例;
1、引用命名空間:using System.IO;
2、注意:使用IO操作文件時,要注意流關閉和釋放問題!
強力推薦:將創建文件流對象的過程寫在using當中,會自動幫助我們釋放資源;
使用try{} catch(Exception ex){} 進行一次捕獲;
3、FileStream 操作字節,可以操作任何類型的文件;下面來簡單介紹FileStream類的方法和參數:
(1)FileStream() 作用:創建FileStream對象,參數:第一個是路徑,第二個是文件模式FileMode枚舉,第三個數據模式FileAcess
FileStream(String, FileMode):
FileStream(String, FileMode, FileAccess)
FileStream(String, FileMode, FileAccess, FileShare)
FileStream(String, FileMode, FileAccess, FileShare, Int32)
初始化FileStream時使用包含文件共享屬性(System.IO.FileShare)的構造函數比使用自定義線程鎖更爲安全和高效
(2)FileMode(以何種方式打開或者創建文件):CreateNew(創建新文件)、Create(創建並覆蓋)、Open(打開)、OpenOrCreate(打開並創建)、Truncate(覆蓋文件)、Append(追加);
(3)FileAcess(文件流對象如何訪問該文件):Read(只讀) 、Write(寫)、ReadWirte(讀寫);
(4)FileShare(進程如何共享文件):None(拒絕共享)、Read 、Write、ReadWrite(同時讀寫)、Delete;
(5)bufferSize(緩衝區大小設置)
4、Stream.Read(array
5、Stream.Write(array
6、close():關閉當前流並釋放與當前流關聯的任何資源(如套接字和文件句柄);
7、dispose():釋放流所有使用的資源;
8、CopyTo(Stream):從當前流中讀取所有字節並將其寫入目標流。
CopyTo(Stream, Int32):從當前流中讀取所有字節,並使用指定的緩衝區大小將它們寫入目標流
9、Seek()(FileStream類維護內部文件指針,該指針指向文件中進行下一次讀寫操作的位置):將此流的當前位置設置爲給定值。(stream.seek(Int64,SeekOrigin)
第一個參數規定文件指針以字節爲單位的移動距離。第二個參數規定開始計算的起始位置;SeekOrigin枚舉包含3個值:Begin、Current 和 End;
例如:aFile.Seek(0, SeekOrigin.End);
10、由於設置了文件共享模式爲允許隨後寫入,所以即使多個線程同時寫入文件,也會等待之前的線程寫入結束之後再執行,而不會出現錯誤
using (FileStream logFile = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
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
{鄭州 不 孕 不 育 醫 院:http://wapyyk.39.net/zz3/zonghe/1d427.html/
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();//清除緩衝區,把所有數據寫入文件中
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);//可以設置第一個參數
//讀入兩百個字節
file.Read(data, 0, (int) file.Length);
//提取字節數組
Decoder dec = Encoding.UTF8.GetDecoder();
dec.GetChars(data, 0, data.Length, charData, 0);
file.Close();
file.Dispose();
return Convert.ToString(charData);
}