ZetCode

C# StreamWriter

last modified July 5, 2023

C# StreamWriter tutorial shows how to write text files in C# with StreamWriter. C# tutorial is a comprehensive tutorial on C# language.

Input & output in C# is based on streams. A Stream is an abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket.

C# StreamWriter

StreamWriter writes characters to a stream in a particular encoding. StreamWriter defaults to UTF-8 encoding unless specified otherwise.

C# StreamWriter example

The WriteLine method writes a new line to the stream. There are several overloaded methods of WriteLine.

Program.cs
var path = "data.txt";

using var sw = new StreamWriter(path);
sw.WriteLine("old falcon");
sw.WriteLine("stormy night");

Console.WriteLine("data written to file");

The example writes two lines to the data.txt file.

using var sw = new StreamWriter(path);

A StreamWriter is created. The using keyword closes the opened file at the end of the Main method.

sw.WriteLine("old falcon");
sw.WriteLine("stormy night");

Two lines are written to the stream.

$ dotnet run
data written to file
$ cat data.txt 
old falcon
stormy night

C# StreamWriter write formatted string

We can write formatted strings with the WriteLine, similarly to string.Format.

Visit C# String Format tutorial to learn more about string formatting in C#.

Program.cs
using FileStream fs = File.Create("data.txt");
using var sw = new StreamWriter(fs);

int n = 3;
string word = "hawks";
sw.WriteLine("There are {0} {1} in the sky", n, word);

Console.WriteLine("data written to file");

The example writes a formatted line to the file.

using FileStream fs = File.Create("data.txt");

A FileStream is created with File.Create.

using var sw = new StreamWriter(fs);

A StreamWriter is generated by passing the FileStream to the constructor of the StreamWriter.

int n = 3;
string word = "hawks";
sw.WriteLine("There are {0} {1} in the sky", n, word);

A formatted line is written to the stream. The {0} and {1} placeholders are replaced with the contents of the n and word variables.

C# StreamWriter append text

We can create a StreamWriter that will append text to the file.

public StreamWriter(string path, bool append);

This constructor 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, the constructor creates a new file.

Program.cs
var path = "words.txt";

using var sw = new StreamWriter(path, true);

char[] data = {'h', 'a', 'w', 'k'};
sw.WriteLine(data);

Console.WriteLine("data appended to file");

The example appends a word to the words.txt file.

using var sw = new StreamWriter(path, true);

The StreamWriter is opened in the Append mode by passing true as the second parameter to the constructor.

char[] data = {'h', 'a', 'w', 'k'};
sw.WriteLine(data);

This overloaded method of WriteLine writes an array of characters to the stream.

Source

StreamWriter class - language reference

In this article we have read text files in C# with StreamWriter.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all C# tutorials.