using System.IO; using System.Text; // Open the file and read its contents using (StreamReader reader = new StreamReader("example.txt", Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } }
using System.IO; // Open the file and write data to it using (StreamWriter writer = new StreamWriter("output.txt")) { writer.WriteLine("Hello, world!"); writer.WriteLine("This is a test."); }This code example uses the StreamWriter class to write data to a file named "output.txt" using the default encoding. Package/library: System.IO.