FileStream.Flush 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
清除此數據流的緩衝區,並導致任何緩衝的數據寫入檔案。
多載
| 名稱 | Description |
|---|---|
| Flush() |
清除此數據流的緩衝區,並導致任何緩衝的數據寫入檔案。 |
| Flush(Boolean) |
清除此數據流的緩衝區,並導致任何緩衝的數據寫入檔案,並清除所有中繼檔案緩衝區。 |
Flush()
清除此數據流的緩衝區,並導致任何緩衝的數據寫入檔案。
public:
override void Flush();
public override void Flush();
override this.Flush : unit -> unit
Public Overrides Sub Flush ()
例外狀況
發生 I/O 錯誤。
溪流已經關閉。
範例
此程式碼範例是該方法更大範例 Lock 的一部分。
// Update the file.
case 'W':
try
{
fileStream.Seek(textLength,
SeekOrigin.Begin);
fileStream.Read(
readText, textLength - 1, byteCount);
tempString = new String(
uniEncoding.GetChars(
readText, textLength - 1, byteCount));
recordNumber = int.Parse(tempString) + 1;
fileStream.Seek(
textLength, SeekOrigin.Begin);
fileStream.Write(uniEncoding.GetBytes(
recordNumber.ToString()),
0, byteCount);
fileStream.Flush();
Console.WriteLine(
"Record has been updated.");
}
| 'W' ->
// Update the file.
try
fileStream.Seek(textLength, SeekOrigin.Begin) |> ignore
fileStream.Read(readText, textLength - 1, byteCount) |> ignore
tempString <- String(uniEncoding.GetChars readText, textLength - 1, byteCount)
recordNumber <- Int32.Parse tempString + 1
fileStream.Seek(textLength, SeekOrigin.Begin) |> ignore
fileStream.Write(string recordNumber |> uniEncoding.GetBytes, 0, byteCount)
fileStream.Flush()
printfn "Record has been updated."
' Update the file.
Case "W"C
Try
aFileStream.Seek(textLength, _
SeekOrigin.Begin)
aFileStream.Read( _
readText, textLength - 1, byteCount)
tempString = New String( _
uniEncoding.GetChars( _
readText, textLength - 1, byteCount))
recordNumber = _
Integer.Parse(tempString) + 1
aFileStream.Seek( _
textLength, SeekOrigin.Begin)
aFileStream.Write(uniEncoding.GetBytes( _
recordNumber.ToString()), 0, byteCount)
aFileStream.Flush()
Console.WriteLine( _
"Record has been updated.")
備註
這個方法會覆寫 Stream.Flush。
當你呼叫該 FileStream.Flush 方法時,作業系統的 I/O 緩衝區也會被清除。
串流的編碼器不會被沖洗,除非你明確呼叫 Flush 或丟棄該物件。 設定 StreamWriter.AutoFlush 為 true 表示資料會從緩衝區沖洗到串流,但編碼器狀態不會被沖洗。 這讓編碼器能保留其狀態(部分字元),以便正確編碼下一個字元區塊。 此情境影響 UTF8 與 UTF7,因為某些字元只能在編碼器接收相鄰字元後再編碼。
由於緩衝區可用於讀取或寫入, Flush() 執行以下兩個功能:
先前寫入緩衝區的資料會被複製到檔案,緩衝區除編碼器狀態外會被清除。
如果 BufferedStream.CanSeek 和
true資料先前已從檔案複製到緩衝區進行讀取,檔案中目前的位置會依緩衝區中未讀位元組數遞減。 然後緩衝區會被清除。
當你想確保中間檔案緩衝區中所有緩衝資料都寫入磁碟時,可以使用 Flush(Boolean) 方法超載。
另請參閱
適用於
Flush(Boolean)
清除此數據流的緩衝區,並導致任何緩衝的數據寫入檔案,並清除所有中繼檔案緩衝區。
public:
virtual void Flush(bool flushToDisk);
public virtual void Flush(bool flushToDisk);
override this.Flush : bool -> unit
Public Overridable Sub Flush (flushToDisk As Boolean)
參數
- flushToDisk
- Boolean
true清除所有中間檔案緩衝區;否則,。 false
備註
當你想確保中間檔案緩衝區中所有緩衝資料都能寫入磁碟時,可以使用這個超載。
當你呼叫該 Flush 方法時,作業系統的 I/O 緩衝區也會被清除。