This documentation is archived and is not being maintained.

BufferedStream.Write Method

Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written.

[Visual Basic]
Overrides Public Sub Write( _
   ByVal array() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
public override void Write(
 byte[] array,
 int offset,
 int count
public: void Write(
 unsigned char array __gc[],
 int offset,
 int count
public override function Write(
   array : Byte[],
 offset : int,
 count : int


The byte array from which to copy count bytes to the current buffered stream.
The offset in the buffer at which to begin copying bytes to the current buffered stream.
The number of bytes to be written to the current buffered stream.


Exception Type Condition
ArgumentException Length of array minus offset is less than count.
ArgumentNullException array is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset or count is negative.
IOException The stream is closed or a null reference (Nothing in Visual Basic).
NotSupportedException The stream does not support writing.
ObjectDisposedException Methods were called after the stream was closed.


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.

Notes to Implementers:  The default implementation on Stream creates a new single-byte array and then calls Write. While this is formally correct, it is inefficient. Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.


[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 | CanWrite | Read | Working with I/O | Reading Text from a File | Writing Text to a File