StreamWriter
System.IO
Writes characters to a stream in a particular encoding. This class provides a convenient way to write text to files and other streams.
Summary
The StreamWriter
class is used to write a stream of characters. It handles the conversion of characters to bytes based on the specified encoding, buffering the output for efficiency, and providing methods for writing strings, characters, and lines.
Constructors
StreamWriter(string path)
Initializes a new instance of the StreamWriter
class for the specified file path. If the file exists, it is overwritten. If the file does not exist, it is created. Default encoding is UTF-8 without BOM.
StreamWriter(string path, bool append)
Initializes a new instance of the StreamWriter
class for the specified file path, and optionally appends to the file. If the file does not exist, it is created. Default encoding is UTF-8 without BOM.
StreamWriter(Stream stream)
Initializes a new instance of the StreamWriter
class for the specified stream, using UTF-8 encoding without a Byte Order Mark (BOM).
StreamWriter(Stream stream, Encoding encoding)
Initializes a new instance of the StreamWriter
class for the specified stream, using the specified encoding.
StreamWriter(Stream stream, Encoding encoding, int bufferSize)
Initializes a new instance of the StreamWriter
class for the specified stream, using the specified encoding and buffer size.
Methods
Write(string value)
Writes a string to the current stream and flushes the stream.
Write(char value)
Writes a character to the current stream and flushes the stream.
WriteLine(string value)
Writes a string to the current stream, followed by a line terminator, and flushes the stream.
WriteLine()
Writes the current line terminator to the stream and flushes the stream.
Flush()
Clears all buffers and causes any buffered data to be written to the underlying stream or file.
Close()
Closes the current StreamWriter
object and the underlying stream.
Properties
Encoding
Gets the Encoding
in which the output is written.
NewLine
Gets or sets the line terminator string used by the current StreamWriter
object.
BaseStream
Gets the underlying stream.
Example
The following example demonstrates how to use StreamWriter
to write text to a file.
using System;
using System.IO;
using System.Text;
public class Example
{
public static void Main()
{
string filePath = "output.txt";
string textToWrite = "Hello, .NET!\nThis is a line written by StreamWriter.";
try
{
// Using statement ensures the StreamWriter is properly disposed
using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8))
{
writer.Write(textToWrite);
writer.WriteLine(" - Appended line.");
writer.WriteLine(); // Writes a blank line
}
Console.WriteLine($"Successfully wrote to {filePath}");
// Optional: Read back to verify
Console.WriteLine("\n--- File Content ---");
using (StreamReader reader = new StreamReader(filePath))
{
Console.WriteLine(reader.ReadToEnd());
}
Console.WriteLine("--------------------");
}
catch (IOException ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}