System.IO.StreamWriter Class

Summary

Writes characters to a stream in a particular encoding. This class cannot be inherited.

Namespace

System.IO

Assembly

mscorlib.dll

Syntax

public sealed class StreamWriter : TextWriter

Remarks

The StreamWriter class provides the functionality to write text to a stream. It handles the conversion of characters to bytes using a specified encoding. When you instantiate a StreamWriter, you typically provide a stream (like a FileStream) to write to. You can also specify the encoding to use.

It's crucial to dispose of a StreamWriter when you are finished with it, either by calling its Dispose method or by using a using statement. This ensures that all buffered data is written to the stream and that the underlying stream is properly closed.

Constructors

Name Description
StreamWriter(Stream) Initializes a new instance of the StreamWriter class for the specified stream, using UTF-8 encoding and the default buffer size.
StreamWriter(Stream, Encoding) Initializes a new instance of the StreamWriter class for the specified stream and encoding, and the default buffer size.
StreamWriter(String) Initializes a new instance of the StreamWriter class for the specified file path, using UTF-8 encoding and the default buffer size.
StreamWriter(String, Boolean) Initializes a new instance of the StreamWriter class for the specified file path, using UTF-8 encoding and the default buffer size. If the file exists, it can be overwritten.
StreamWriter(String, Boolean, Encoding) Initializes a new instance of the StreamWriter class for the specified file path, encoding, and buffer size.

Methods

Name Description
Write(String) Writes a string to the current stream and advances the current position in the stream according to the current encoding.
WriteLine(String) Writes a string followed by a line terminator to the current stream.
Flush() Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
Close() Closes the current StreamWriter object and the underlying stream.

Properties

Name Description
Encoding Gets the Encoding in which the output is written.
NewLine Gets or sets the line terminator string used by the current TextWriter.
BaseStream Gets the underlying stream.

StreamWriter(Stream)

Initializes a new instance of the StreamWriter class for the specified stream, using UTF-8 encoding and the default buffer size.

public StreamWriter(Stream stream)

StreamWriter(Stream, Encoding)

Initializes a new instance of the StreamWriter class for the specified stream and encoding, and the default buffer size.

public StreamWriter(Stream stream, Encoding encoding)

StreamWriter(String)

Initializes a new instance of the StreamWriter class for the specified file path, using UTF-8 encoding and the default buffer size. If the file exists, it is overwritten.

public StreamWriter(string path)

StreamWriter(String, Boolean)

Initializes a new instance of the StreamWriter class for the specified file path, using UTF-8 encoding and the default buffer size. If the file exists, it can be overwritten.

public StreamWriter(string path, bool append)

StreamWriter(String, Boolean, Encoding)

Initializes a new instance of the StreamWriter class for the specified file path, encoding, and buffer size.

public StreamWriter(string path, bool append, Encoding encoding)

Write(String)

Writes a string to the current stream and advances the current position in the stream according to the current encoding.

public override void Write(string value)

Parameters

  • value: The string to write.

WriteLine(String)

Writes a string followed by a line terminator to the current stream.

public override void WriteLine(string value)

Parameters

  • value: The string to write.

Flush()

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

public override void Flush()

Close()

Closes the current StreamWriter object and the underlying stream.

public override void Close()

Encoding

Gets the Encoding in which the output is written.

public override Encoding Encoding { get; }

NewLine

Gets or sets the line terminator string used by the current TextWriter.

public override string NewLine { get; set; }

BaseStream

Gets the underlying stream.

protected Stream BaseStream { get; }