C Sharp TextWriter

C# TextWriter

Being an abstract class, the C# TextWriter class is found in System.IO namespace to be used for writing text or sequential series of characters into a file.

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class content  
    {  
        static void Main(string[] args)  
        {  
            using (TextWriter writer = File.CreateText("d:\\file.txt"))  
            {  
                writer.WriteLine("Hello World."); 
                writer.WriteLine("Good Morning.");  
                writer.WriteLine("How are you?");  
                writer.WriteLine("Today is a great day.");  
                writer.WriteLine("Good Luck.");  
 
            }  
            Console.WriteLine("Success!!");  
        }  
    }  
}

Console Output:

D:\\file.txt:

Explanation:

In the above example, we are using the TextWriter class to write five lines data into a file.

 

Please Share