C# System.IO.FileStream

爲文件提供 Stream,既支持同步讀寫操做,也支持異步讀寫操做。android

 

 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 類的新實例。ios

FileStream(IntPtr, FileAccess, Boolean)

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

FileStream(IntPtr, FileAccess, Boolean, Int32)

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

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

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

FileStream(SafeFileHandle, FileAccess)

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

FileStream(SafeFileHandle, FileAccess, Int32)

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

FileStream(SafeFileHandle, FileAccess, Int32, Boolean)

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

FileStream(String, FileMode)

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

FileStream(String, FileMode, FileAccess)

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

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)
相關文章
相關標籤/搜索