當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# File.OpenWrite()用法及代碼示例

File.OpenWrite(String)是一個內置的File類方法,用於打開現有文件或創建一個新文件進行寫入。

用法:

public static System.IO.FileStream OpenWrite (string path);

參數:該函數接受如下所示的參數:



  • path: This is the specified text file which is going to be opened for writing.

異常:

  • UnauthorizedAccessException:調用者沒有所需的權限。或路徑指定了隻讀文件或目錄。
  • ArgumentException:路徑為長度為零的字符串,僅包含空格,或者由InvalidPathChars定義的一個或多個無效字符。
  • ArgumentNullException:路徑為空。
  • PathTooLongException:指定的路徑,文件名或兩者都超過了system-defined的最大長度。
  • DirectoryNotFoundException:指定的路徑無效。
  • NotSupportedException:路徑格式無效。

返回值:在具有寫訪問權限的指定路徑上返回未共享的FileStream對象。

下麵是說明File.OpenWrite(String)方法的程序。

程序1:最初,沒有創建文件。下麵的代碼本身創建了一個文件gfg.txt,向其中寫入一些文本並進行打印。

// C# program to illustrate the usage 
// of File.OpenWrite(String) method 
  
// Using System, System.IO and 
// System.Text namespaces 
using System; 
using System.IO; 
using System.Text; 
  
class GFG { 
    public static void Main() 
    { 
        // Creating a file 
        string path = @"gfg.txt"; 
  
        // Opening the file for writing 
        using(FileStream fs = File.OpenWrite(path)) 
        { 
            // Putting below specified contents 
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a computer science portal."); 
            fs.Write(info, 0, info.Length); 
        } 
  
        // Opening the file for reading 
        using(FileStream fs = File.OpenRead(path)) 
        { 
            byte[] b = new byte[1024]; 
            UTF8Encoding temp = new UTF8Encoding(true); 
  
            while (fs.Read(b, 0, b.Length) > 0) { 
                // Printing the contents of the file 
                Console.WriteLine(temp.GetString(b)); 
            } 
        } 
    } 
}

輸出:

GFG is a computer science portal.

上麵的代碼給出上麵的輸出,並創建一個文件,如下所示:

gfg.txt

程序2:最初,將創建一個文件file.txt,其內容如下所示-

file.txt

下麵的代碼將用其他指定的內容覆蓋文件內容,然後將打印最終內容。

// C# program to illustrate the usage 
// of File.OpenWrite(String) method 
  
// Using System, System.IO and 
// System.Text namespaces 
using System; 
using System.IO; 
using System.Text; 
  
class GFG { 
    public static void Main() 
    { 
        // Specifing a file 
        string path = @"file.txt"; 
  
        // Opening the file for writing 
        using(FileStream fs = File.OpenWrite(path)) 
        { 
            // Overwriting the above file with below 
            // specified contents 
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS portal."); 
            fs.Write(info, 0, info.Length); 
        } 
  
        // Opening the file for reading 
        using(FileStream fs = File.OpenRead(path)) 
        { 
            byte[] b = new byte[1024]; 
            UTF8Encoding temp = new UTF8Encoding(true); 
  
            while (fs.Read(b, 0, b.Length) > 0) { 
                // Printing the final contents of the file 
                Console.WriteLine(temp.GetString(b)); 
            } 
        } 
    } 
}

輸出:

GFG is a CS portal.



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 File.OpenWrite() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。