Class StreamWriter
Implements a TextWriter for writing characters to a stream in a particular encoding.
Implements
Inherited Members
Namespace: System.IO
Assembly: System.IO.Streams.dll
Syntax
public class StreamWriter : TextWriter, IDisposable
Constructors
StreamWriter(Stream)
Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.
Declaration
public StreamWriter(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream to write to. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
|
Properties
BaseStream
Gets the underlying stream that interfaces with a backing store.
Declaration
public virtual Stream BaseStream { get; }
Property Value
Type | Description |
---|---|
Stream | The stream this StreamWriter is writing to. |
Encoding
Gets the Encoding in which the output is written.
Declaration
public override Encoding Encoding { get; }
Property Value
Type | Description |
---|---|
Encoding | The Encoding specified in the constructor for the current instance, or UTF8Encoding if an encoding was not specified. |
Overrides
Methods
Close()
Closes the current StreamWriter object and the underlying stream.
Declaration
public override void Close()
Overrides
Remarks
This method overrides Close(). This implementation of Close() calls the Dispose(bool) method passing a true value. You must call Close() to ensure that all data is correctly written out to the underlying stream.Following a call to Close(), any operations on the StreamWriter might raise exceptions. If there is insufficient space on the disk, calling Close() will raise an exception. Flushing the stream will not flush its underlying encoder unless you explicitly call Flush() or Close().
Dispose(bool)
Causes any buffered data to be written to the underlying stream, releases the unmanaged resources used by the StreamWriter, and optionally the managed resources.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | true to release both managed and unmanaged resources; false to release only unmanaged resources. |
Overrides
Remarks
When the disposing parameter is true, this method releases all resources held by any managed objects that this StreamWriter references. This method invokes the Dispose(bool) method of each referenced object.
Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Declaration
public override void Flush()
Overrides
Remarks
This method overrides TextWriter.Flush. Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close().
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | The current writer is closed. |
IOException | An I/O error has occurred. |
Write(char)
Writes a character to the stream.
Declaration
public override void Write(char value)
Parameters
Type | Name | Description |
---|---|---|
char | value |
Overrides
Remarks
This method overrides Write(char). The specified character is written to the underlying stream unless the end of the stream is reached prematurely.
WriteLine()
Writes a line terminator to the text stream.
Declaration
public override void WriteLine()
Overrides
WriteLine(string)
Writes a string to the stream, followed by a line terminator.
Declaration
public override void WriteLine(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value |
Overrides
Remarks
This overload is equivalent to the Write(string) overload. The line terminator is defined by the CoreNewLine field. This method does not search the specified string for individual newline characters(hexadecimal 0x000a) and replace them with NewLine.