Namespace: System.IO

StreamWriter

public class StreamWriter : TextWriter
Represents a text writer that provides a convenient way to write a sequence of strings or bytes to a stream.

Constructors

Methods

Properties

Example

using System.IO; using System.Text; public class Example { public static void Main() { string path = @"c:\temp\MyTest.txt"; using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8)) { sw.WriteLine("This is the first line."); sw.WriteLine("This is the second line."); sw.Write("This is a continuation of the second line."); sw.Write(" And this is another part."); sw.WriteLine(); // Adds a blank line sw.WriteLine("End of file."); } // Verify the content Console.WriteLine($"Content written to {path}"); } }