System.IO.StreamWriter Class

Implements a System.IO.TextWriter for writing characters to a stream in a particular encoding.

See Also: StreamWriter Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class StreamWriter : TextWriter

Remarks

System.IO.StreamWriter is designed for character output in a particular encoding, whereas classes derived from System.IO.Stream are designed for byte input and output.

System.IO.StreamWriter defaults to using an instance of System.Text.UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its System.Text.Encoding.GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the System.Text.Encoding.UTF8 property. To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter.#ctor(string, bool, System.Text.Encoding) or StreamWriter.#ctor(Stream, System.Text.Encoding).

By default, a System.IO.StreamWriter is not thread safe. See TextWriter.Synchronized(TextWriter) for a thread-safe wrapper.

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

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0