Bewerken

Share via


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.

public:
 override void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public override void Write (string format, object? arg0, object? arg1, object? arg2);
override this.Write : string * obj * obj * obj -> unit
Public Overrides Sub Write (format As String, arg0 As Object, arg1 As Object, arg2 As Object)

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

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.

public:
 override void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public override void Write (string format, object? arg0, object? arg1);
override this.Write : string * obj * obj -> unit
Public Overrides Sub Write (format As String, arg0 As Object, arg1 As Object)

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

Write(Char[], Int32, Int32)

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

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

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

See also

Applies to

Write(String, ReadOnlySpan<Object>)

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

public:
 override void Write(System::String ^ format, ReadOnlySpan<System::Object ^> arg);
public override void Write (string format, scoped ReadOnlySpan<object?> arg);
override this.Write : string * ReadOnlySpan<obj> -> unit
Public Overrides Sub Write (format As String, arg As ReadOnlySpan(Of Object))

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

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.

public:
 override void Write(System::String ^ format, ... cli::array <System::Object ^> ^ arg);
public override void Write (string format, params object?[] arg);
override this.Write : string * obj[] -> unit
Public Overrides Sub Write (format As String, ParamArray arg As Object())

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

Write(ReadOnlySpan<Char>)

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

Writes a character span to the stream.

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>

The character span to write.

Applies to

Write(String)

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

Writes a string to the stream.

public:
 override void Write(System::String ^ value);
public override void Write (string value);
public override void Write (string? value);
override this.Write : string -> unit
Public Overrides Sub Write (value As String)

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

Write(Char[])

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

Writes a character array to the stream.

public:
 override void Write(cli::array <char> ^ buffer);
public override void Write (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

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

Write(Char)

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

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

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

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.

public:
 override void Write(System::String ^ format, System::Object ^ arg0);
public override void Write (string format, object? arg0);
override this.Write : string * obj -> unit
Public Overrides Sub Write (format As String, arg0 As Object)

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