StreamWriter in C#

The StreamWriter class in C# is used to write characters to a stream in a particular encoding. It provides an easy way to create and write text to files, making it essential for file I/O operations.

StreamWriter automatically handles file creation and provides various methods to write text data efficiently.

Syntax

Following is the syntax for creating a StreamWriter object −

StreamWriter writer = new StreamWriter("filename.txt");

Using the using statement ensures automatic disposal −

using (StreamWriter writer = new StreamWriter("filename.txt")) {
   writer.WriteLine("text");
}

Basic File Writing with StreamWriter

With StreamWriter, you can create a new file and write text to it. The following example demonstrates basic file writing −

using System;
using System.IO;

public class Program {
   public static void Main() {
      using (StreamWriter sw = new StreamWriter("hello.txt")) {
         sw.WriteLine("Hello");
         sw.WriteLine("World");
         sw.WriteLine("Welcome to C# StreamWriter!");
      }
      
      // Read and display the created file content
      string content = File.ReadAllText("hello.txt");
      Console.WriteLine("File content:");
      Console.WriteLine(content);
   }
}

The output of the above code is −

File content:
Hello
World
Welcome to C# StreamWriter!

Using Different Write Methods

StreamWriter provides several methods to write text data. Here's an example showing different writing methods −

using System;
using System.IO;

public class Program {
   public static void Main() {
      using (StreamWriter sw = new StreamWriter("methods.txt")) {
         sw.Write("This is ");
         sw.Write("written without ");
         sw.WriteLine("a new line at the end.");
         sw.WriteLine("This starts on a new line.");
         sw.WriteAsync("Async write operation.");
      }
      
      string content = File.ReadAllText("methods.txt");
      Console.WriteLine("File content:");
      Console.WriteLine(content);
   }
}

The output of the above code is −

File content:
This is written without a new line at the end.
This starts on a new line.
Async write operation.

Appending to Existing Files

You can append text to an existing file by specifying true as the second parameter −

using System;
using System.IO;

public class Program {
   public static void Main() {
      // Create initial file
      using (StreamWriter sw = new StreamWriter("append.txt")) {
         sw.WriteLine("Initial content");
      }
      
      // Append to the file
      using (StreamWriter sw = new StreamWriter("append.txt", true)) {
         sw.WriteLine("Appended content");
         sw.WriteLine("More appended text");
      }
      
      string content = File.ReadAllText("append.txt");
      Console.WriteLine("Final file content:");
      Console.WriteLine(content);
   }
}

The output of the above code is −

Final file content:
Initial content
Appended content
More appended text

Common StreamWriter Methods

Method Description
Write() Writes text without adding a new line
WriteLine() Writes text and adds a new line character
WriteAsync() Asynchronously writes text to the stream
Flush() Clears all buffers and writes data to the file
Close() Closes the StreamWriter and releases resources

Conclusion

StreamWriter in C# provides an efficient way to write text data to files. It automatically handles file creation, supports various writing methods, and works seamlessly with the using statement for proper resource management. Always use using statements to ensure files are properly closed and resources are released.

Updated on: 2026-03-17T07:04:35+05:30

271 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements