FileStream.Flush FileStream.Flush FileStream.Flush FileStream.Flush Method

定义

清除此流的缓冲区,使得所有缓冲数据都写入到文件中。Clears buffers for this stream and causes any buffered data to be written to the file.

重载

Flush() Flush() Flush() Flush()

清除此流的缓冲区,使得所有缓冲数据都写入到文件中。Clears buffers for this stream and causes any buffered data to be written to the file.

Flush(Boolean) Flush(Boolean) Flush(Boolean) Flush(Boolean)

清除此流的缓冲区,将所有缓冲数据都写入到文件中,并且也清除所有中间文件缓冲区。Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.

Flush() Flush() Flush() Flush()

清除此流的缓冲区,使得所有缓冲数据都写入到文件中。Clears buffers for this stream and causes any buffered data to be written to the file.

public:
 override void Flush();
public override void Flush ();
override this.Flush : unit -> unit
Public Overrides Sub Flush ()

异常

出现 I/O 错误。An I/O error occurred.

示例

此代码示例是为Lock方法提供的一个更大示例的一部分。This code example is part of a larger example provided for the Lock method.

// 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.");
    }
' 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.FlushThis method overrides Stream.Flush.

调用FileStream.Flush方法时,也会刷新操作系统 i/o 缓冲区。When you call the FileStream.Flush method, the operating system I/O buffer is also flushed.

除非显式调用Flush或释放对象,否则不会刷新流的编码器。A stream's encoder is not flushed unless you explicitly call Flush or dispose of the object. 如果StreamWriter.AutoFlush设置true为,则表示将数据从缓冲区刷新到流,但不会刷新编码器状态。Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. 这允许编码器保留其状态 (部分字符), 以使其能够正确编码下一个字符块。This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. 此方案影响 UTF8 和 UTF7, 其中某些字符只能在编码器接收到相邻字符后编码。This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

由于缓冲区可用于读取或写入, Flush()因此执行以下两个函数:Because a buffer can be used for either reading or writing, Flush() performs the following two functions:

  • 先前写入缓冲区的任何数据都将复制到该文件中,并且除其编码器状态外,还会清除该缓冲区。Any data previously written to the buffer is copied to the file and the buffer is cleared except for its encoder state.

  • 如果BufferedStream.CanSeektrue ,并且数据以前已从文件复制到缓冲区进行读取,则文件中的当前位置将按缓冲区中的未读字节数递减。If BufferedStream.CanSeek is true and data was previously copied from the file to the buffer for reading, the current position within the file is decremented by the number of unread bytes in the buffer. 然后,将清除该缓冲区。The buffer is then cleared.

若要确保将中间文件缓冲区中的所有缓冲数据写入磁盘,请使用方法重载。Flush(Boolean)Use the Flush(Boolean) method overload when you want to ensure that all buffered data in intermediate file buffers is written to disk.

另请参阅

Flush(Boolean) Flush(Boolean) Flush(Boolean) Flush(Boolean)

清除此流的缓冲区,将所有缓冲数据都写入到文件中,并且也清除所有中间文件缓冲区。Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.

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 Boolean Boolean Boolean

如果刷新所有中间文件缓冲区,则为 true;否则为 falsetrue to flush all intermediate file buffers; otherwise, false.

注解

若要确保将中间文件缓冲区中的所有缓冲数据写入磁盘,请使用此重载。Use this overload when you want to ensure that all buffered data in intermediate file buffers is written to disk.

调用Flush方法时,也会刷新操作系统 i/o 缓冲区。When you call the Flush method, the operating system I/O buffer is also flushed.

另请参阅

适用于