On this page
Description
Represents a text writer for a stream. This class provides methods for writing characters to a stream in a buffered manner. It supports encoding and automatic flushing.
StreamWriter
is used to write characters to a stream. It is particularly useful when you need to write text data to files or other streams with control over encoding and buffering.
Syntax
Constructors
The StreamWriter
class has several constructors to initialize an instance with different parameters, such as the stream, encoding, and whether to flush automatically.
StreamWriter(Stream stream)
Initializes a new instance of the StreamWriter
class for the specified stream, using UTF-8 encoding and the buffer size of 1024 bytes.
StreamWriter(Stream stream, Encoding encoding)
Initializes a new instance of the StreamWriter
class for the specified stream and encoding, using the default buffer size.
StreamWriter(string path)
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.
And many more overloads for various configurations.
Properties
Name | Description |
---|---|
AutoFlush |
Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream automatically after each write operation. |
BaseStream |
Gets the underlying stream that the StreamWriter is writing to. |
Encoding |
Gets the Encoding in which the output is written. |
Format |
Gets or sets the formatting for the current StreamWriter instance. |
NewLine |
Gets or sets the newline characters, which is \r\n on Windows, \n on Unix, and \r\n on Macintosh. |
Methods
StreamWriter
inherits many methods from TextWriter
, including methods for writing characters, strings, and formatted strings.
Write(char value)
Writes a character to the current stream and flushes the buffer.
Write(string value)
Writes a string to the current stream and flushes the buffer.
WriteLine()
Writes a newline character to the stream and flushes the buffer.
WriteLine(string value)
Writes a string followed by a newline character to the stream and flushes the buffer.
Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Close()
Closes the StreamWriter
and the underlying stream.
Example
Writing to a text file
This example demonstrates how to use StreamWriter
to write strings to a text file.
using System.Text;
public class Example
{
public static void Main(string[] args)
{
string filePath = @"c:\temp\myTextFile.txt";
try
{
// Use 'using' statement to ensure the StreamWriter is disposed properly
using (StreamWriter sw = new StreamWriter(filePath,true))
{
sw.WriteLine("This is the first line.");
sw.Write("This is ");
sw.WriteLine("a second line with WriteLine.");
sw.Flush();
}
}
// The file is automatically closed when leaving the 'using' block
}
}
Requirements
Platform | Version |
---|---|
.NET | Core 1.0, 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0, 7.0, 8.0 |
.NET Framework | 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 |