Write and read text file using StreamWriter : Text File Read Write : File Directory Stream C# Examples


C# Examples » File Directory Stream » Text File Read Write »

 

Write and read text file using StreamWriter









    
using  System;
using  System.IO;

public  class  MainClass
{
        public  static  int  Main(string[]  args)
        {
        FileInfo  f  =  new  FileInfo("test.txt");

        StreamWriter  writer  =  f.CreateText();
        writer.WriteLine("string  1");
        writer.WriteLine("string  2");
        
        for(int  i  =  0;  i  <  10;  i++)
        {
            writer.Write(i  +  "  ");
        }
        writer.Write(writer.NewLine);

        writer.Close();

        //  Now  read  it  all  back  in.
        StreamReader  sr  =  File.OpenText("test.txt");

        string  input  =  null;
        while  ((input  =  sr.ReadLine())  !=  null)
        {
            Console.WriteLine  (input);
        }

                return  0;
        }
}
    
   
  
   



Output

string 1
string 2
0 1 2 3 4 5 6 7 8 9


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo File Directory Stream
» Text File Read Write