StreamWriter.Write Method

Definition

Overloads

Write(Char)

Writes a character to the stream.

Write(Char[])

Writes a character array to the stream.

Write(String)

Writes a string to the stream.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the stream.

Write(Char)

Writes a character to the stream.

public override void Write (char value);
Parameters
value
Char

The character to write to the stream.

Exceptions

An I/O error occurs.

AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

Remarks

This method overrides TextWriter.Write.

The specified character is written to the underlying stream unless the end of the stream is reached prematurely. If AutoFlush is true, Flush is invoked automatically.

For a list of common I/O tasks, see Common I/O Tasks.

Write(Char[])

Writes a character array to the stream.

public override void Write (char[] buffer);
Parameters
buffer
Char[]

A character array containing the data to write. If buffer is null, nothing is written.

Exceptions

An I/O error occurs.

AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

Remarks

This method overrides TextWriter.Write.

The specified characters are written to the underlying stream unless the end of the stream is reached prematurely. If AutoFlush is true, Flush is invoked automatically.

This method might provide faster performance than Write (char[],``int,``int) because it has fewer arguments to check.

For a list of common I/O tasks, see Common I/O Tasks.

Write(String)

Writes a string to the stream.

public override void Write (string value);
Parameters
value
String

The string to write to the stream. If value is null, nothing is written.

Exceptions

AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

An I/O error occurs.

Remarks

This method overrides TextWriter.Write.

The specified String is written to the underlying stream unless the end of the stream is reached prematurely.

Flush is invoked automatically if AutoFlush is true. If value is null, no entries are written.

For a list of common I/O tasks, see Common I/O Tasks.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the stream.

public override void Write (char[] buffer, int index, int count);
Parameters
buffer
Char[]

A character array that contains the data to write.

index
Int32

The character position in the buffer at which to start reading data.

count
Int32

The maximum number of characters to write.

Exceptions

buffer is null.

The buffer length minus index is less than count.

index or count is negative.

An I/O error occurs.

AutoFlush is true or the StreamWriter buffer is full, and current writer is closed.

AutoFlush is true or the StreamWriter buffer is full, and the contents of the buffer cannot be written to the underlying fixed size stream because the StreamWriter is at the end the stream.

Examples

This example writes eight characters from a 13-element array to a file, beginning at the third element of the array.

using namespace System;
using namespace System::IO;

int main()
{
   FileStream^ sb = gcnew FileStream( "MyFile.txt",FileMode::OpenOrCreate );
   array<Char>^b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
   StreamWriter^ sw = gcnew StreamWriter( sb );
   sw->Write( b, 3, 8 );
   sw->Close();
}
using System;
using System.IO;
 
public class SWBuff 
{
    public static void Main(String[] args)
    {
        FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
        char[] b = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
        StreamWriter sw = new StreamWriter(sb);
        sw.Write(b, 3, 8);
        sw.Close();
    }
}
Imports System
Imports System.IO

Public Class SWBuff

    Public Shared Sub Main()
        Dim sb As New FileStream("MyFile.txt", FileMode.OpenOrCreate)
        Dim b As Char() = {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, _
           "h"c, "i"c, "j"c, "k"c, "l"c, "m"c}
        Dim sw As New StreamWriter(sb)
        sw.Write(b, 3, 8)
        sw.Close()
    End Sub
End Class

Remarks

This method overrides TextWriter.Write.

The characters are read from buffer beginning at index and continuing through index + (count - 1). All characters are written to the underlying stream unless the end of the underlying stream is reached prematurely. Flush is invoked automatically if AutoFlush is true.

For a list of common I/O tasks, see Common I/O Tasks.