C# System.IO.FileStream


為文件提供 Stream,既支持同步讀寫操作,也支持異步讀寫操作。

 

 1 using System;
 2 using System.IO;
 3 using System.Text;
 4 
 5 class Test
 6 {
 7 
 8     public static void Main()
 9     {
10         string path = @"c:\temp\MyTest.txt";
11 
12         // Delete the file if it exists.
13         if (File.Exists(path))
14         {
15             File.Delete(path);
16         }
17 
18         //Create the file.
19         using (FileStream fs = File.Create(path))
20         {
21             AddText(fs, "This is some text");
22             AddText(fs, "This is some more text,");
23             AddText(fs, "\r\nand this is on a new line");
24             AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");
25 
26             for (int i=1;i < 120;i++)
27             {
28                 AddText(fs, Convert.ToChar(i).ToString());
29 
30             }
31         }
32 
33         //Open the stream and read it back.
34         using (FileStream fs = File.OpenRead(path))
35         {
36             byte[] b = new byte[1024];
37             UTF8Encoding temp = new UTF8Encoding(true);
38             while (fs.Read(b,0,b.Length) > 0)
39             {
40                 Console.WriteLine(temp.GetString(b));
41             }
42         }
43     }
44 
45     private static void AddText(FileStream fs, string value)
46     {
47         byte[] info = new UTF8Encoding(true).GetBytes(value);
48         fs.Write(info, 0, info.Length);
49     }
50 }

 

構造函數 

FileStream(IntPtr, FileAccess)

使用指定的讀/寫權限為指定的文件句柄初始化 FileStream 類的新實例。

FileStream(IntPtr, FileAccess, Boolean)

使用指定的讀/寫權限和 FileStream 實例所屬權為指定的文件句柄初始化 FileStream 類的新實例。

FileStream(IntPtr, FileAccess, Boolean, Int32)

使用指定的讀/寫權限、FileStream 實例所屬權和緩沖區大小為指定的文件句柄初始化 FileStream 類的新實例。

FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)

使用指定的讀/寫權限、FileStream 實例所屬權、緩沖區大小和同步或異步狀態為指定的文件句柄初始化 FileStream 類的新實例。

FileStream(SafeFileHandle, FileAccess)

使用指定的讀/寫權限為指定的文件句柄初始化 FileStream 類的新實例。

FileStream(SafeFileHandle, FileAccess, Int32)

使用指定的讀/寫權限和緩沖區大小為指定的文件句柄初始化 FileStream 類的新實例。

FileStream(SafeFileHandle, FileAccess, Int32, Boolean)

使用指定的讀/寫權限、緩沖區大小和同步或異步狀態為指定的文件句柄初始化FileStream 類的新實例。

FileStream(String, FileMode)

使用指定的路徑和創建模式初始化 FileStream 類的新實例。

FileStream(String, FileMode, FileAccess)

使用指定的路徑、創建模式和讀/寫權限初始化 FileStream 類的新實例。

FileStream(String, FileMode, FileAccess, FileShare)

使用指定的路徑、創建模式、讀/寫權限和共享權限創建 FileStream 類的新實例。

FileStream(String, FileMode, FileAccess, FileShare, Int32)

用指定的路徑、創建模式、讀/寫及共享權限和緩沖區大小初始化 FileStream 類的新實例。

FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)

使用指定的路徑、創建模式、讀/寫和共享權限、緩沖區大小和同步或異步狀態初始化 FileStream 類的新實例。

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)

使用指定的路徑、創建模式、讀/寫和共享權限、其他 FileStreams 可以具有的對此文件的訪問權限、緩沖區大小和附加文件選項初始化 FileStream 類的新實例。

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)

使用指定的路徑、創建模式、訪問權限和共享權限、緩沖區大小和附加文件選項初始化 FileStream 類的新實例。

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

使用指定的路徑、創建模式、訪問權限和共享權限、緩沖區大小、附加文件選項、訪問控制和審核安全初始化 FileStream 類的新實例。

屬性 

CanRead

獲取一個值,該值指示當前流是否支持讀取。

CanSeek

獲取一個值,該值指示當前流是否支持查找。

CanTimeout

獲取一個值,該值確定當前流是否可以超時。

(Inherited from Stream)
CanWrite

獲取一個值,該值指示當前流是否支持寫入。

Handle

獲取當前 FileStream 對象所封裝文件的操作系統文件句柄。

IsAsync

獲取一個值,它指示 FileStream 是異步打開還是同步打開的。

Length

獲取用字節表示的流長度。

Name

獲取 FileStream 中已打開的文件的絕對路徑。

Position

獲取或設置此流的當前位置。

ReadTimeout

獲取或設置一個值(以毫秒為單位),該值確定流在超時前嘗試讀取多長時間。

(Inherited from Stream)
SafeFileHandle

獲取 SafeFileHandle 對象,它代表當前 FileStream 對象所封裝的文件的操作系統文件句柄。

WriteTimeout

獲取或設置一個值(以毫秒為單位),該值確定流在超時前嘗試寫入多長時間。

(Inherited from Stream)

方法 

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

開始異步讀操作。 請考慮改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

開始異步寫操作。 請考慮改用 WriteAsync(Byte[], Int32, Int32, CancellationToken)

CopyTo(Stream)

從當前流中讀取字節並將其寫入到另一流中。

(Inherited from Stream)
CopyTo(Stream, Int32)

使用指定的緩沖區大小,從當前流中讀取字節並將其寫入到另一流中。

(Inherited from Stream)
CopyToAsync(Stream)

從當前流中異步讀取字節並將其寫入到另一個流中。

(Inherited from Stream)
CopyToAsync(Stream, CancellationToken) Inherited from Stream
CopyToAsync(Stream, Int32)

使用指定的緩沖區大小,從當前流中異步讀取字節並將其寫入到另一流中。

(Inherited from Stream)
CopyToAsync(Stream, Int32, CancellationToken)

使用指定的緩沖區大小和取消令牌,從當前流中異步讀取字節並將其寫入到另一個流中。

(Inherited from Stream)
CreateObjRef(Type)

創建一個對象,該對象包含生成用於與遠程對象進行通信的代理所需的全部相關信息。

(Inherited from MarshalByRefObject)
CreateWaitHandle()

分配 WaitHandle 對象。

(Inherited from Stream)
Dispose()

釋放由 Stream 使用的所有資源。

(Inherited from Stream)
Dispose(Boolean)

釋放由 FileStream 占用的非托管資源,還可以另外再釋放托管資源。

EndRead(IAsyncResult)

等待掛起的異步讀操作完成。 (請考慮改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。)

EndWrite(IAsyncResult)

結束異步寫入操作,在 I/O 操作完成之前一直阻止。 (請考慮改用WriteAsync(Byte[], Int32, Int32, CancellationToken)。)

Equals(Object)

確定指定的對象是否等於當前對象。

(Inherited from Object)
Finalize()

確保垃圾回收器回收 FileStream 時釋放資源並執行其他清理操作。

Flush()

清除此流的緩沖區,使得所有緩沖數據都寫入到文件中。

Flush(Boolean)

清除此流的緩沖區,將所有緩沖數據都寫入到文件中,並且也清除所有中間文件緩沖區。

FlushAsync()

異步清除此流的所有緩沖區並導致所有緩沖數據都寫入基礎設備中。

(Inherited from Stream)
FlushAsync(CancellationToken)

異步清理這個流的所有緩沖區,並使所有緩沖數據寫入基礎設備,並且監控取消請求。

GetAccessControl()

獲取 FileSecurity 對象,該對象封裝當前 FileStream 對象所描述的文件的訪問控制列表 (ACL) 項。

GetHashCode()

作為默認哈希函數。

(Inherited from Object)
GetLifetimeService()

檢索控制此實例的生存期策略的當前生存期服務對象。

(Inherited from MarshalByRefObject)
GetType()

獲取當前實例的 Type

(Inherited from Object)
InitializeLifetimeService()

獲取生存期服務對象來控制此實例的生存期策略。

(Inherited from MarshalByRefObject)
Lock(Int64, Int64)

防止其他進程讀取或寫入 FileStream

MemberwiseClone()

創建當前 Object 的淺表副本。

(Inherited from Object)
MemberwiseClone(Boolean)

創建當前 MarshalByRefObject 對象的淺表副本。

(Inherited from MarshalByRefObject)
ObjectInvariant()

提供對 Contract 的支持。

(Inherited from Stream)
Read(Byte[], Int32, Int32)

從流中讀取字節塊並將該數據寫入給定緩沖區中。

ReadAsync(Byte[], Int32, Int32)

從當前流異步讀取字節序列,並將流中的位置提升讀取的字節數。

(Inherited from Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

從當前流異步讀取字節的序列,將流中的位置提升讀取的字節數,並監視取消請求。

ReadByte()

從文件中讀取一個字節,並將讀取位置提升一個字節。

Seek(Int64, SeekOrigin)

將該流的當前位置設置為給定值。

SetAccessControl(FileSecurity)

將 FileSecurity 對象所描述的訪問控制列表 (ACL) 項應用於當前 FileStream 對象所描述的文件。

SetLength(Int64)

將該流的長度設置為給定值。

ToString()

返回表示當前對象的字符串。

(Inherited from Object)
Unlock(Int64, Int64)

允許其他進程訪問以前鎖定的某個文件的全部或部分。

Write(Byte[], Int32, Int32)

將字節塊寫入文件流。

WriteAsync(Byte[], Int32, Int32)

將字節序列異步寫入當前流,並將流的當前位置提升寫入的字節數。

(Inherited from Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

將字節的序列異步寫入當前流,將該流中的當前位置向前移動寫入的字節數,並監視取消請求。

WriteByte(Byte)

一個字節寫入文件流中的當前位置。

IDisposable.Dispose()

釋放由 Stream 使用的所有資源。

(Inherited from Stream)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM