Stream.Flush 方法


当在派生类中重写时,将清除该流的所有缓冲区,并使得所有缓冲数据被写入到基础设备。When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

 abstract void Flush();
public abstract void Flush ();
abstract member Flush : unit -> unit
Public MustOverride Sub Flush ()


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


用于Flush实现缓冲区的流上的重写。Override Flush on streams that implement a buffer. 使用此方法可将任何信息从基础缓冲区移到其目标,清除缓冲区,或同时移动两者。Use this method to move any information from an underlying buffer to its destination, clear the buffer, or both. 根据对象的状态,可能需要修改流中的当前位置(例如,如果基础流支持查找)。Depending upon the state of the object, you might have to modify the current position within the stream (for example, if the underlying stream supports seeking). 有关其他信息,请参见 CanSeekFor additional information see CanSeek.

在从Stream不支持写入的派生的类中Flush ,通常实现为空方法以确保与其他Stream类型完全兼容,因为它可以刷新只读流。In a class derived from Stream that doesn't support writing, Flush is typically implemented as an empty method to ensure full compatibility with other Stream types since it's valid to flush a read-only stream.

使用StreamWriter StreamBinaryWriter类时,请不要刷新基对象。When using the StreamWriter or BinaryWriter class, do not flush the base Stream object. 请改用类的FlushClose方法,这可确保先将数据刷新到基础流,然后再将数据写入文件。Instead, use the class's Flush or Close method, which makes sure that the data is flushed to the underlying stream first and then written to the file.