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.AutoFlushtrue意味着数据从缓冲区刷新到流中,但编码器状态不会进行刷新。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.

另请参阅

适用于