StreamWriter.Write StreamWriter.Write StreamWriter.Write StreamWriter.Write Method

Definition

Writes data to the stream.

Overloads

Write(Char) Write(Char) Write(Char) Write(Char)

Writes a character to the stream.

Write(Char[]) Write(Char[]) Write(Char[]) Write(Char[])

Writes a character array to the stream.

Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>)
Write(String) Write(String) Write(String) Write(String)

Writes a string to the stream.

Write(Char[], Int32, Int32) Write(Char[], Int32, Int32) Write(Char[], Int32, Int32) Write(Char[], Int32, Int32)

Writes a subarray of characters to the stream.

Write(Char) Write(Char) Write(Char) Write(Char)

Writes a character to the stream.

public:
 override void Write(char value);
public override void Write (char value);
override this.Write : char -> unit
Public Overrides Sub Write (value As Char)
Parameters
value
Char Char Char Char

The character to write to the stream.

Exceptions

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[]) Write(Char[]) Write(Char[]) Write(Char[])

Writes a character array to the stream.

public:
 override void Write(cli::array <char> ^ buffer);
public override void Write (char[] buffer);
override this.Write : char[] -> unit
Public Overrides Sub Write (buffer As Char())
Parameters
buffer
Char[]

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

Exceptions

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(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>) Write(ReadOnlySpan<Char>)

public:
 override void Write(ReadOnlySpan<char> buffer);
public override void Write (ReadOnlySpan<char> buffer);
override this.Write : ReadOnlySpan<char> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Char))
Parameters
buffer
ReadOnlySpan<Char>

Write(String) Write(String) Write(String) Write(String)

Writes a string to the stream.

public:
 override void Write(System::String ^ value);
public override void Write (string value);
override this.Write : string -> unit
Public Overrides Sub Write (value As String)
Parameters
value
String String String 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 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 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) Write(Char[], Int32, Int32) Write(Char[], Int32, Int32) Write(Char[], Int32, Int32)

Writes a subarray of characters to the stream.

public:
 override void Write(cli::array <char> ^ buffer, int index, int count);
public override void Write (char[] buffer, int index, int count);
override this.Write : char[] * int * int -> unit
Public Overrides Sub Write (buffer As Char(), index As Integer, count As Integer)
Parameters
buffer
Char[]

A character array that contains the data to write.

index
Int32 Int32 Int32 Int32

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

count
Int32 Int32 Int32 Int32

The maximum number of characters to write.

Exceptions

The buffer length minus index is less than count.

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.

Applies to