Open In App

File.CreateText() Method in C# with Examples

File.CreateText() is an inbuilt File class method that is used to overwrite the contents of the existing file with the given UTF-8 encoded text and if the file is not created already, this function will create a new file with the specified contents.

Syntax: 



public static System.IO.StreamWriter CreateText (string path);

Parameter: This function accepts a parameter which is illustrated below: 

  • Path: This is the file where UTF-8 encoded texts are going to be overwritten. The file is created if it doesn’t already exist.

Exceptions: 



Return Value: Returns a StreamWriter that writes to the specified file using UTF-8 encoding.
Below are the programs to illustrate the File.CreateText() method.

Program 1: 
Before running the below code, a file file.txt is created with some contents which is shown below:
 




// C# program to illustrate the usage
// of File.CreateText() method
 
// Using System, System.IO namespaces
using System;
using System.IO;
 
class GFG {
    // Main method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
 
        // Overwriting to the above existing file
        using(StreamWriter sw = File.CreateText(myfile))
        {
            sw.WriteLine("GeeksforGeeks");
            sw.WriteLine("is a");
            sw.WriteLine("computer science");
            sw.WriteLine("portal.");
        }
 
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(myfile))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
    }
}

Executing: 

mcs -out:main.exe main.cs
mono main.exe
GeeksforGeeks
is a
computer science
portal.

After running the above code, above output is shown and the existing file file.txt becomes like below:

Program 2: Initially, no file is created and hence below code itself create a file named as file.txt




// C# program to illustrate the usage
// of File.CreateText() method
 
// Using System, System.IO namespaces
using System;
using System.IO;
 
class GFG {
    // Main method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
 
        // Checking the existence of above file
        if (!File.Exists(myfile)) {
            // Creating a new file with below contents
            using(StreamWriter sw = File.CreateText(myfile))
            {
                sw.WriteLine("Geeks");
                sw.WriteLine("GFG");
                sw.WriteLine("GeeksforGeeks");
            }
        }
 
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(myfile))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
    }
}

Executing: 

mcs -out:main.exe main.cs
mono main.exe
Geeks
GFG
GeeksforGeeks

After running the above code, a new file file.txt is created which is shown below:

 


Article Tags :
C#