StreamWriter.Write Method

Definition

Writes data to the stream.

Overloads

Write(String, Object, Object, Object)

Writes a formatted string to the stream, using the same semantics as the Format(String, Object, Object, Object) method.

Write(String, Object, Object)

Writes a formatted string to the stream using the same semantics as the Format(String, Object, Object) method.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the stream.

Write(String, ReadOnlySpan<Object>)

Writes a formatted string to the stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

Write(String, Object[])

Writes a formatted string to the stream, using the same semantics as the Format(String, Object[]) method.

Write(ReadOnlySpan<Char>)

Writes a character span to the stream.

Write(String)

Writes a string to the stream.

Write(Char[])

Writes a character array to the stream.

Write(Char)

Writes a character to the stream.

Write(String, Object)

Writes a formatted string to the stream, using the same semantics as the Format(String, Object) method.

Write(String, Object, Object, Object)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a formatted string to the stream, using the same semantics as the Format(String, Object, Object, Object) method.

C#
public override void Write(string format, object? arg0, object? arg1, object? arg2);

Parameters

format
String

A composite format string.

arg0
Object

The first object to format and write.

arg1
Object

The second object to format and write.

arg2
Object

The third object to format and write.

Remarks

See Write(String, Object, Object, Object) for a description of the composite formatting capabilities offered.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10

Write(String, Object, Object)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a formatted string to the stream using the same semantics as the Format(String, Object, Object) method.

C#
public override void Write(string format, object? arg0, object? arg1);

Parameters

format
String

A composite format string.

arg0
Object

The first object to format and write.

arg1
Object

The second object to format and write.

Remarks

See Write(String, Object, Object) for a description of the composite formatting capabilities offered.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10

Write(Char[], Int32, Int32)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a subarray of characters to the stream.

C#
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.

C#
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();
    }
}

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(String, ReadOnlySpan<Object>)

Writes a formatted string to the stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

C#
public override void Write(string format, scoped ReadOnlySpan<object?> arg);

Parameters

format
String

A composite format string.

arg
ReadOnlySpan<Object>

An object span that contains zero or more objects to format and write.

Applies to

.NET 10 and .NET 9
Product Versions
.NET 9, 10

Write(String, Object[])

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a formatted string to the stream, using the same semantics as the Format(String, Object[]) method.

C#
public override void Write(string format, params object?[] arg);

Parameters

format
String

A composite format string.

arg
Object[]

An object array that contains zero or more objects to format and write.

Remarks

See Write(String, Object[]) for a description of the composite formatting capabilities offered.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10

Write(ReadOnlySpan<Char>)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a character span to the stream.

C#
public override void Write(ReadOnlySpan<char> buffer);

Parameters

buffer
ReadOnlySpan<Char>

The character span to write.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

Write(String)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a string to the stream.

C#
public override void Write(string value);
C#
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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Char[])

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a character array to the stream.

C#
public override void Write(char[] buffer);
C#
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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(Char)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a character to the stream.

C#
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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Write(String, Object)

Source:
StreamWriter.cs
Source:
StreamWriter.cs
Source:
StreamWriter.cs

Writes a formatted string to the stream, using the same semantics as the Format(String, Object) method.

C#
public override void Write(string format, object? arg0);

Parameters

format
String

A composite format string.

arg0
Object

The object to format and write.

Remarks

See Write(String, Object) for a description of the composite formatting capabilities offered.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10