C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
StreamWriter: We pass the FileStream object we get from File.Create to a StreamWriter. We then use WriteLine to write to the file.
StreamWriterC# program that uses FileStream, File.Create
using System;
using System.IO;
class Program
{
static void Main()
{
// Use FileStream with File.Create.
// ... Pass FileStream to a StreamWriter and write data to it.
using (FileStream fileStream =
File.Create(@"C:\programs\example1.txt"))
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine("Example 1 written");
}
Console.WriteLine("DONE");
}
}
Output
DONE
Contents of example1.txt:
Example 1 written
And: Often the FileStream serves as a "base" to other streams we might use. The example above shows this.