File Reader/Writer in C#
Below is an example of how you can use C#'s StreamReader and StreamWriter classes to read from and write to a file.
using System; using System.IO; class FileReadWriteExample { static void Main() { // Replace "input.txt" and "output.txt" with your file names string inputFile = "input.txt"; string outputFile = "output.txt"; // Reading from a file using StreamReader try { using (StreamReader reader = new StreamReader(inputFile)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine("Read from file: " + line); // You can process the line as needed here } } } catch (IOException e) { Console.Error.WriteLine("Error reading from file: " + e.Message); } // Writing to a file using StreamWriter try { using (StreamWriter writer = new StreamWriter(outputFile)) { // Replace the following line with the content you want to write string contentToWrite = "Hello, this is written to the file!"; writer.WriteLine(contentToWrite); Console.WriteLine("Write to file: " + contentToWrite); } } catch (IOException e) { Console.Error.WriteLine("Error writing to file: " + e.Message); } } }