using (StreamWriter writer = new StreamWriter("example.txt")) { writer.Write("Hello"); writer.Flush(); writer.Write(" World!"); }
using (FileStream fs = new FileStream("example.dat", FileMode.Create)) using (StreamWriter writer = new StreamWriter(fs)) { // write data to the stream writer.Flush(); }In this example, we create a new `FileStream` with `FileMode.Create` to create a new file named "example.dat". We then create a new `StreamWriter` that writes to this file. Before closing the stream, we call `Flush` to ensure any buffered data is written to the file. The `StreamWriter.Flush` method is part of the System.IO namespace in the .NET Framework Class Library.