C# File IO - C# StreamReader

StreamReader and StreamWriter are found in the System.IO namespace. Both classes are useful when you want to read or write character based data.

Methods:



Example 


using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication1

{

  class Program

  {

    static void Main(string[] args)

    {

      string[] names = new string[] { "Nitin Kumar", "Prashant KUmar" };



      using (StreamWriter sw = new StreamWriter("D:\\BrothersName.txt"))

      {



        foreach (string s in names)

        {

          sw.WriteLine(s);

        }

      }



      // Read and show each line from the file.

      string line = "";

      using (StreamReader sr = new StreamReader("D:\\BrothersName.txt"))

      {

        while ((line = sr.ReadLine()) != null)

        {

          Console.WriteLine(line);

        }

      }

      Console.ReadKey();

    }

  }



}





































































Output:

it will create BrothersName.txt file on the given location with data.