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


C# File.WriteAllLines(String, String[], Encoding)用法及代碼示例

File.WriteAllLines(String, String[], Encoding)是一個內置的 File 類方法,用於創建一個新文件,使用指定的編碼將指定的字符串數組寫入文件,然後關閉文件。
用法:

public static void WriteAllLines (string path, string[] contents, System.Text.Encoding encoding); 
 

參數:此函數接受三個參數,如下所示:

  • path: This is the specified file where specified string array are going to be written.
  • contents: This is the specified string array to write to the file.
  • encoding: This represents the character encoding applied to the string array.

異常:

  • ArgumentException:路徑是一個長度為零的字符串,隻包含空格,或者一個或多個由 InvalidPathChars 定義的無效字符。
  • ArgumentNullException:路徑或內容為空。
  • PathTooLongException:指定的路徑,文件名或兩者都超過了system-defined的最大長度。
  • DirectoryNotFoundException:指定的路徑無效。
  • IOException:打開文件時發生I /O錯誤。
  • UnauthorizedAccessException:路徑指定了一個隻讀文件。或者路徑指定了一個隱藏的文件。 OR 當前平台不支持此操作。或者路徑指定了一個目錄。或者調用者沒有所需的權限。
  • NotSupportedException:路徑格式無效。
  • SecurityException:調用者沒有所需的權限。

下麵是說明 File.WriteAllLines(String, String[]) 方法的程序。
程序1:最初,沒有創建文件。下麵的代碼本身創建一個文件 file.txt 並將指定的字符串數組寫入文件。



C#


// C# program to illustrate the usage
// of File.WriteAllLines(String, String[], 
// Encoding) 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
        string path = @"file.txt";
  
        // Creating some string array to
        // write into the file
        string[] createText = { "GFG", "is a", "CS portal." };
  
        // Calling WriteAllLines() function to write
        // the specified string array into the file
        File.WriteAllLines(path, createText, Encoding.UTF8);
  
        // Reading the file contents
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach(string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}

輸出:

GFG
is a
CS portal.

運行上麵的代碼後,顯示了上麵的輸出,並創建了一個新文件 file.txt 如下所示 -

file.txt

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

file.txt

下麵的代碼用指定的字符串數組覆蓋文件內容。

C#


// C# program to illustrate the usage
// of File.WriteAllLines(String, 
// String[], Encoding) 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
        string path = @"file.txt";
  
        // Creating some string array to
        // write into the file
        string[] createText = { "GFG", "Geeks", "GeeksforGeeks" };
  
        // Calling WriteAllLines() function to overwrite
        // the specified string array into the file
        File.WriteAllLines(path, createText, Encoding.UTF8);
  
        // Reading the file contents
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach(string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}

輸出:

GFG
Geeks
GeeksforGeeks

運行上麵的代碼後,顯示了上麵的輸出,文件file.txt的內容變成了下圖——

file.txt




相關用法


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