This documentation is archived and is not being maintained.

BufferedStream.Flush Method

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

[Visual Basic]
Overrides Public Sub Flush()
public override void Flush();
public: void Flush();
public override function Flush();


Exception Type Condition
IOException The data source or repository is not open.


For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close.

If you use the BufferedStream(Stream, Int32) constructor, thus specifying the buffer size while creating the BufferedStream object, the content is flushed when it reaches the buffer size. For example, code such as BufferedStream bs

= new BufferedStream(bs, 5) will flush the content when the buffer size reaches 5 bytes.

All the read and write methods of BufferedStream automatically maintain the buffer, so there is no need to invoke Flush when switching back and forth between reading and writing.


[Visual Basic, C#, C++] This code example is part of a larger example provided for the BufferedStream class.

[Visual Basic] 
' Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.")
startTime = DateTime.Now
For i As Integer = 1 To numberOfLoops
    bufStream.Write(dataToSend, 0, dataToSend.Length)
Next i

bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes sent In {1} seconds." & vbCrLf, _
    numberOfLoops * dataToSend.Length, _

// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
    bufStream.Write(dataToSend, 0, dataToSend.Length);
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
    numberOfLoops * dataToSend.Length, 

// Send the data using the BufferedStream.
Console::WriteLine(S"Sending data using BufferedStream.");
startTime = DateTime::Now;
for(int i = 0; i < numberOfLoops; i++)
    bufStream->Write(dataToSend, 0, dataToSend->Length);
bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine(S"{0} bytes sent in {1} seconds.\n",
    (numberOfLoops * dataToSend->Length).ToString(), 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

BufferedStream Class | BufferedStream Members | System.IO Namespace | Close | Working with I/O | Reading Text from a File | Writing Text to a File