Open In App

File.Create(String, Int32) Method in C# with Examples

File.Create(String, Int32) is an inbuilt File class method which is used to overwrite an existing file, specifying a buffer size else create a new file if the specified file is not existing.
Syntax: 
 

public static System.IO.FileStream Create (string path, int bufferSize);

Parameter: This function accepts two parameters which are illustrated below: 
 



  • path: This is the specified file path.
  • bufferSize: This is the specified buffer size.

Exceptions:
 

Below are the programs to illustrate the File.Create(String, Int32) method.
Program 1: Initially, no file is created. Below code itself create a new file file.txt with the specified contents.
 






// C# program to illustrate the usage
// of File.Create(String, Int32) method
 
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    public static void Main()
    {
        // Specifying a file path
        string file_path = @"file.txt";
 
        try {
            // Creating a new file with below
            // specified contents
            using(FileStream fs = File.Create(file_path, 1024))
            {
                // Adding some info into the file
                byte[] info = new UTF8Encoding(true).GetBytes("GeeksforGeeks");
                fs.Write(info, 0, info.Length);
            }
 
            // Reading the file contents
            using(StreamReader sr = File.OpenText(file_path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) {
                    Console.WriteLine(s);
                }
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}

Executing: 
 

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

After running the above code, above output is shown and a new file file.txt is created with some specified contents shown below:
 

Program 2: The below shown file file.txt is created before running the below code. 
 

 




// C# program to illustrate the usage
// of File.Create(String) method
 
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    public static void Main()
    {
        // Specifying a file path
        string file_path = @"file.txt";
 
        try {
            // Overwriting the above file contents
            using(FileStream fs = File.Create(file_path, 1024))
            {
                // Adding some info into the file
                byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS Portal");
                fs.Write(info, 0, info.Length);
            }
 
            // Reading the file contents
            using(StreamReader sr = File.OpenText(file_path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) {
                    Console.WriteLine(s);
                }
            }
        }
 
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}

Executing: 
 

mcs -out:main.exe main.cs
mono main.exe
GFG is a CS Portal

After running the above code, the above output is shown, and existing file contents get overwritten.
 

 


Article Tags :
C#