FileStream.Flush 方法

定義

清除這個資料流的緩衝區,讓所有緩衝資料全部寫入檔案。

多載

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 = gcnew String( uniEncoding->GetChars( readText, textLength - 1, byteCount ) );
      recordNumber = Int32::Parse( tempString ) + 1;
      fileStream->Seek( textLength, SeekOrigin::Begin );
      fileStream->Write( uniEncoding->GetBytes( recordNumber.ToString() ), 0, byteCount );
      fileStream->Flush();
      Console::WriteLine( "Record has been updated." );
   }
// 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.AutoFlushtrue 表示資料會從緩衝區排清到資料流程,但不會排清編碼器狀態。 這可讓編碼器保持其狀態 (部分字元) ,以便正確地編碼下一個字元區塊。 此案例會影響 UTF8 和 UTF7,其中某些字元只能在編碼器收到相鄰字元或字元之後進行編碼。

因為緩衝區可用於讀取或寫入, Flush() 所以會執行下列兩個函式:

  • 先前寫入緩衝區的任何資料都會複製到檔案,而且緩衝區會清除,但編碼器狀態除外。

  • 如果 先前 BufferedStream.CanSeektrue 已將資料從檔案複製到緩衝區以供讀取,檔案中的目前位置會由緩衝區中未讀取的位元組數目遞減。 接著會清除緩衝區。

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 緩衝區。

另請參閱

適用於