C# FileStream類
在 C# 語言中檔案讀寫流使用 FileStream 類來表示,FileStream 類主要用于檔案的讀寫,不僅能讀寫普通的文本檔案,還可以讀取影像檔案、聲音檔案等不同格式的檔案,區別于File類的是它對檔案可進行分步讀寫,減小記憶體壓力,缺點是我們需要手動的關閉和釋放資源,
FileAccess
FileAccess 列舉型別主要用于設定檔案的訪問方式,具體的列舉值如下,
- Read:以只讀方式打開檔案,
- Write:以寫方式打開檔案,
- ReadWrite:以讀寫方式打開檔案,
FileMode
FileMode 列舉型別主要用于設定檔案打開或創建的方式,具體的列舉值如下,
- CreateNew:創建新檔案,如果檔案已經存在,則會拋出例外,
- Create:創建檔案,如果檔案不存在,則洗掉原來的檔案,重新創建檔案,
- Open:打開已經存在的檔案,如果檔案不存在,則會拋出例外,
- OpenOrCreate:打開已經存在的檔案,如果檔案不存在,則創建檔案,
- Truncate:打開已經存在的檔案,并清除檔案中的內容,保留檔案的創建日期,如果檔案不存在,則會拋出例外,
- Append:打開檔案,用于向檔案中追加內容,如果檔案不存在,則創建一個新檔案,
FileShare
FileShare 列舉型別主要用于設定多個物件同時訪問同一個檔案時的訪問控制,具體的列舉值如下,
- None:謝絕共享當前的檔案,
- Read:允許隨后打開檔案讀取資訊,
- ReadWrite:允許隨后打開檔案讀寫資訊,
- Write:允許隨后打開檔案寫入資訊,
- Delete:允許隨后洗掉檔案,
- Inheritable:使檔案句柄可由子行程繼承,
FileOptions
FileOptions 列舉型別用于設定檔案的高級選項,包括檔案是否加密、訪問后是否洗掉等,具體的列舉值如下,
- WriteThrough:指示系統應通過任何中間快取、直接寫入磁盤,
- None:指示在生成 System.IO.FileStream 物件時不應使用其他選項,
- Encrypted:指示檔案是加密的,只能通過用于加密的同一用戶賬戶來解密,
- DeleteOnClose:指示當不再使用某個檔案時自動洗掉該檔案,
- SequentialScan:指示按從頭到尾的順序訪問檔案,
- RandomAccess:指示隨機訪問檔案,
- Asynchronous:指示檔案可用于異步讀取和寫入,
FileStream 類的構造方法有很多,這里介紹一些常用的構造方法,如下表所示,
| 構造方法 | 說明 |
|---|---|
| FileStream(string path, FileMode mode) | 使用指定路徑的檔案、檔案模式創建 FileStream 類的實體 |
| FileStream(string path, FileMode mode, FileAccess access) | 使用指定路徑的檔案、檔案打開模式、檔案訪問模式創建 FileStream 類的實體 |
| FileStream(string path, FileMode mode, FileAccess access, FileShare share) | 使用指定的路徑、創建模式、讀寫權限和共享權限創建 FileStream 類的一個新實體 |
| FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) | 使用指定的路徑、創建模式、讀寫權限和共享權限、其他 檔案選項創建 FileStream 類的實體 |
FileStream
| 屬性或方法 | 作用 |
|---|---|
| bool CanRead | 只讀屬性,獲取一個值,該值指示當前流是否支持讀取 |
| bool CanSeek | 只讀屬性,獲取一個值,該值指示當前流是否支持查找 |
| bool CanWrite | 只讀屬性,獲取一個值,該值指示當前流是否支持寫入 |
| bool IsAsync | 只讀屬性,獲取一個值,該值指示 FileStream 是異步還 是同步打開的 |
| long Length | 只讀屬性,獲取用位元組表示的流長度 |
| string Name | 只讀屬性,獲取傳遞給構造方法的 FileStream 的名稱 |
| long Position | 屬性,獲取或設定此流的當前位置 |
| int Read(byte[] array, int offset, int count) | 從流中讀取位元組塊并將該資料寫入給定緩沖區中 |
| int ReadByte() | 從檔案中讀取一個位元組,并將讀取位置提升一個位元組 |
| long Seek(lorig offset, SeekOrigin origin) | 將該流的當前位置設定為給定值 |
| void Lock(long position, long length) | 防止其他行程讀取或寫入 System.IO.FileStream |
| void Unlock(long position, long length) | 允許其他行程訪問以前鎖定的某個檔案的全部或部分 |
| void Write(byte[] array, int offset, int count) | 將位元組塊寫入檔案流 |
| void WriteByte(byte value) | 將一個位元組寫入檔案流中的當前位置 |
File和FileStream的區別
File是一個靜態類;FileStream是一個非靜態類,
File:是一個檔案的類,對檔案進行操作,其內部封裝了對檔案的各種操作(MSDN:提供用于創建、復制、洗掉、移動和打開單一檔案的靜態方法,并協助創建FileStream物件),
FileStream:檔案流的類,對txt,xml,avi等檔案進行內容寫入、讀取、復制...時候需要使用的一個工具,
創建實體
string path = "D:\\test.txt";
FileStream fileStream1 = **new** FileStream(path, FileMode.Open);
FileStream fileStream2 = **new** FileStream(path, FileMode.Open, FileAccess.Read);
FileStream fileStream3 = **new** FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
FileStream fileStream4 = **new** FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 10, FileOptions.None);
//定義檔案路徑
string path = @"D:\\code\\test.txt";
//創建 FileStream 類的實體
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
//定義屬性
string msg = "11111";
//將字串轉換為位元組陣列
byte[] bytes = Encoding.UTF8.GetBytes(msg);
//向檔案中寫入位元組陣列
fileStream.Write(bytes, 0, bytes.Length);
//重繪緩沖區
fileStream.Flush();
//關閉流
fileStream.Close();
class Program
{
static void Main(string[] args)
{
//定義檔案路徑
string path = @"D:\\code\\test.txt";
//判斷是否含有指定檔案
if (File.Exists(path))
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
//定義存放檔案資訊的位元組陣列
byte[] bytes = new byte[fileStream.Length];
//讀取檔案資訊
fileStream.Read(bytes, 0, bytes.Length);
//將得到的位元組型陣列重寫編碼為字符型陣列
char[] c = Encoding.UTF8.GetChars(bytes);
//輸出
Console.WriteLine(c);
//關閉流
fileStream.Close();
}
else
{
Console.WriteLine("不存在!");
}
}
}
檔案復制
public class FileSreamTest
{
public static void CopyFile(string source,string target)
{
//讀取檔案流
using (FileStream fsRead = new FileStream(source,FileMode.Open,FileAccess.Read))
{
//寫入流
using (FileStream fsWrite = new FileStream(target,FileMode.OpenOrCreate,FileAccess.Write))
{
byte[] buffer = new byte[1024*1024*5];
while (true)
{
//回傳讀取位元組數
int r= fsRead.Read(buffer,0,buffer.Length);
if (r==0)
{
break;
}
//寫入
fsWrite.Write(buffer,0,r);
}
}
}
}
}
static void Main(string[] args)
{
string source =@"E:\功能流程.7z";
string target=@"C:\Users\CH190006\Desktop\Test\功能流程.7z";
FileSreamTest.CopyFile(source,target);
Console.ReadKey();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/46966.html
標籤:C#
上一篇:C# File檔案類
