You are on page 1of 11

2/19/2021 StreamWriter Class (System.

IO) | Microsoft Docs

StreamWriter Class

Namespace: System.IO
Assembly: System.Runtime.dll

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


In this article
Definition
Examples
Remarks
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 1/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs

Constructors
Fields
Properties
Methods
Explicit Interface Implementations
Extension Methods
Applies to
See also

C# = Copy

public class StreamWriter : System.IO.TextWriter

Inheritance Object 9 MarshalByRefObject 9 TextWriter 9 StreamWriter

Examples
The following example shows how to use a StreamWriter object to write a file that lists the
directories on the C drive, and then uses a StreamReader object to read and display each
directory name. A good practice is to use these objects in a using statement so that the
unmanaged resources are correctly disposed. The using statement automatically calls
Dispose on the object when the code that is using it has completed. The constructor used
in this example is not supported for use in Windows Store Apps.

= Copy
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StreamReadWrite
{
class Program
{
static void Main(string[] args)
{
// Get the directories currently on the C drive.
DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();

// Write each directory name to a file.


https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 2/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs

using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))


{
foreach (DirectoryInfo dir in cDirs)
{
sw.WriteLine(dir.Name);
}
}

// Read and show each line from the file.


string line = "";
using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}

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

) Important

This type implements the IDisposable interface. When you have finished using the
type, you should dispose of it either directly or indirectly. To dispose of the type
directly, call its Dispose method in a try / catch block. To dispose of it indirectly, use a
language construct such as using (in C#) or Using (in Visual Basic). For more
information, see the "Using an Object that Implements IDisposable" section in the
IDisposable interface topic.

StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise.


This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its
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 Encoding.UTF8 property. To specify a
BOM and determine whether an exception is thrown on invalid bytes, use a constructor
th t t di bj t t
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 h St W it (St i B l 3/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean,
Encoding) or StreamWriter.

By default, a StreamWriter is not thread safe. See TextWriter.Synchronized for a thread-safe


wrapper.

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

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.

StreamWriter(Stream, Initializes a new instance of the StreamWriter class for the


Encoding) specified stream by using the specified encoding and the default
buffer size.

StreamWriter(Stream, Initializes a new instance of the StreamWriter class for the


Encoding, Int32) specified stream by using the specified encoding and buffer size.

StreamWriter(Stream, Initializes a new instance of the StreamWriter class for the


Encoding, Int32, Boolean) specified stream by using the specified encoding and buffer size,
and optionally leaves the stream open.

StreamWriter(String) Initializes a new instance of the StreamWriter class for the


specified file by using the default encoding and buffer size.

StreamWriter(String, Boolean) Initializes a new instance of the StreamWriter class for the
specified file by using the default encoding and buffer size. If the
file exists, it can be either overwritten or appended to. If the file
does not exist, this constructor creates a new file.

StreamWriter(String, Boolean, Initializes a new instance of the StreamWriter class for the
Encoding) specified file by using the specified encoding and default buffer
size. If the file exists, it can be either overwritten or appended to.
If the file does not exist, this constructor creates a new file.

StreamWriter(String, Boolean, Initializes a new instance of the StreamWriter class for the
Encoding, Int32) specified file on the specified path, using the specified encoding
and buffer size. If the file exists, it can be either overwritten or
appended to. If the file does not exist, this constructor creates a
new file.

https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 4/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs

Fields
CoreNewLine Stores the newline characters used for this TextWriter .
(Inherited from TextWriter)

Null Provides a StreamWriter with no backing store that can be


written to, but not read from.

Properties
AutoFlush Gets or sets a value indicating whether the StreamWriter will
flush its buffer to the underlying stream after every call to
Write(Char).

BaseStream Gets the underlying stream that interfaces with a backing store.

Encoding Gets the Encoding in which the output is written.

FormatProvider Gets an object that controls formatting.


(Inherited from TextWriter)

NewLine Gets or sets the line terminator string used by the current
TextWriter .
(Inherited from TextWriter)

Methods
Close() Closes the current StreamWriter object and the underlying
stream.

CreateObjRef(Type) Creates an object that contains all the relevant information


required to generate a proxy used to communicate with a
remote object.
(Inherited from MarshalByRefObject)

Dispose() Releases all resources used by the TextWriter object.


(Inherited from TextWriter)

Dispose(Boolean) Causes any buffered data to be written to the underlying stream,


releases the unmanaged resources used by the StreamWriter,
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 5/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs

and optionally the managed resources.

DisposeAsync() Asynchronously writes any buffered data to the underlying


stream and releases the unmanaged resources used by the
StreamWriter.

Equals(Object) Determines whether the specified object is equal to the current


object.
(Inherited from Object)

Flush() Clears all buffers for the current writer and causes any buffered
data to be written to the underlying stream.

FlushAsync() Clears all buffers for this stream asynchronously and causes any
buffered data to be written to the underlying device.

GetHashCode() Serves as the default hash function.


(Inherited from Object)

GetLifetimeService() Obsolete.
Retrieves the current lifetime service object that controls the
lifetime policy for this instance.
(Inherited from MarshalByRefObject)

GetType() Gets the Type of the current instance.


(Inherited from Object)

InitializeLifetimeService() Obsolete.

Obtains a lifetime service object to control the lifetime policy for


this instance.
(Inherited from MarshalByRefObject)

MemberwiseClone() Creates a shallow copy of the current Object.


(Inherited from Object)

MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject


object.
(Inherited from MarshalByRefObject)

ToString() Returns a string that represents the current object.


(Inherited from Object)

Write(Boolean) Writes the text representation of a Boolean value to the text


stream.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 6/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs

(Inherited from TextWriter)

Write(Char) Writes a character to the stream.

Write(Char[]) Writes a character array to the stream.

Write(Char[], Int32, Int32) Writes a subarray of characters to the stream.

Write(Decimal) Writes the text representation of a decimal value to the text


stream.
(Inherited from TextWriter)

Write(Double) Writes the text representation of an 8-byte floating-point value


to the text stream.
(Inherited from TextWriter)

Write(Int32) Writes the text representation of a 4-byte signed integer to the


text stream.
(Inherited from TextWriter)

Write(Int64) Writes the text representation of an 8-byte signed integer to the


text stream.
(Inherited from TextWriter)

Write(Object) Writes the text representation of an object to the text stream by


calling the ToString method on that object.
(Inherited from TextWriter)

Write(ReadOnlySpan<Char>) Writes a character span to the stream.

Write(Single) Writes the text representation of a 4-byte floating-point value to


the text stream.
(Inherited from TextWriter)

Write(String) Writes a string 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) Writes a formatted string to the stream using the same
semantics as the Format(String, Object, Object) method..

Write(String, Object, Object, Writes a formatted string to the stream, using the same
Object) semantics as the Format(String Object Object Object) method
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 7/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
Object) semantics as the Format(String, Object, Object, Object) method.

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

Write(StringBuilder) Writes a string builder to the text stream.


(Inherited from TextWriter)

Write(UInt32) Writes the text representation of a 4-byte unsigned integer to


the text stream.
(Inherited from TextWriter)

Write(UInt64) Writes the text representation of an 8-byte unsigned integer to


the text stream.
(Inherited from TextWriter)

WriteAsync(Char) Asynchronously writes a character to the stream.

WriteAsync(Char[]) Writes a character array to the text stream asynchronously.


(Inherited from TextWriter)

WriteAsync(Char[], Int32, Int32) Asynchronously writes a subarray of characters to the stream.

WriteAsync(ReadOnly Asynchronously writes a character memory region to the stream.


Memory<Char>, Cancellation
Token)

WriteAsync(String) Asynchronously writes a string to the stream.

WriteAsync(StringBuilder, Asynchronously writes a string builder to the text stream.


CancellationToken) (Inherited from TextWriter)

WriteLine() Writes a line terminator to the text stream.


(Inherited from TextWriter)

WriteLine(Boolean) Writes the text representation of a Boolean value to the text


stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(Char) Writes a character to the text stream, followed by a line


terminator.
(Inherited from TextWriter)

WriteLine(Char[]) Writes an array of characters to the text stream, followed by a


line terminator
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 8/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
line terminator.
(Inherited from TextWriter)

WriteLine(Char[], Int32, Int32) Writes a subarray of characters to the text stream, followed by a
line terminator.
(Inherited from TextWriter)

WriteLine(Decimal) Writes the text representation of a decimal value to the text


stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(Double) Writes the text representation of a 8-byte floating-point value to


the text stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(Int32) Writes the text representation of a 4-byte signed integer to the


text stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(Int64) Writes the text representation of an 8-byte signed integer to the


text stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(Object) Writes the text representation of an object to the text stream, by


calling the ToString method on that object, followed by a line
terminator.
(Inherited from TextWriter)

WriteLine(ReadOnly Writes the text representation of a character span to the stream,

Span<Char>) followed by a line terminator.

WriteLine(Single) Writes the text representation of a 4-byte floating-point value to


the text stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(String) Writes a string to the stream, followed by a line terminator.

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

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

WriteLine(String, Object, Writes out a formatted string and a new line to the stream, using
Object Object) the same semantics as Format(String Object)
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 9/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs
Object, Object) the same semantics as Format(String, Object).

WriteLine(String, Object[]) Writes out a formatted string and a new line to the stream, using
the same semantics as Format(String, Object).

WriteLine(StringBuilder) Writes the text representation of a string builder to the text


stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(UInt32) Writes the text representation of a 4-byte unsigned integer to


the text stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLine(UInt64) Writes the text representation of an 8-byte unsigned integer to


the text stream, followed by a line terminator.
(Inherited from TextWriter)

WriteLineAsync() Asynchronously writes a line terminator to the stream.

WriteLineAsync(Char) Asynchronously writes a character to the stream, followed by a


line terminator.

WriteLineAsync(Char[]) Asynchronously writes an array of characters to the text stream,


followed by a line terminator.
(Inherited from TextWriter)

WriteLineAsync(Char[], Int32, Asynchronously writes a subarray of characters to the stream,


Int32) followed by a line terminator.

WriteLineAsync(ReadOnly Asynchronously writes the text representation of a character


Memory<Char>, Cancellation memory region to the stream, followed by a line terminator.
Token)

WriteLineAsync(String) Asynchronously writes a string to the stream, followed by a line


terminator.

WriteLineAsync(StringBuilder, Asynchronously writes the text representation of a string builder


CancellationToken) to the text stream, followed by a line terminator.
(Inherited from TextWriter)

Explicit Interface Implementations


IDisposable.Dispose() For a description of this member, see Dispose().
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 10/11
2/19/2021 StreamWriter Class (System.IO) | Microsoft Docs

(Inherited from TextWriter)

Extension Methods
ConfigureAwait(IAsync Configures how awaits on the tasks returned from an async
Disposable, Boolean) disposable are performed.

Applies to
Product Versions

.NET 5.0

.NET Core 1.0, 1.1, 2.0, 2.1, 2.2, 3.0, 3.1

.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

.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1

UWP 10.0

Xamarin.Android 7.1

Xamarin.iOS 10.8

Product Versions

Xamarin.Mac 3.0

See also
Stream
StreamReader
Encoder
Encoding
File and Stream I/O
How to: Read Text from a File
How to: Write Text to a File
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=net-5.0 11/11

You might also like