C#程式建立檔案並寫入檔案


簡介

建立檔案並在其中寫入資料是檔案處理的基礎。在這裡,我們將討論一種使用 C# 程式建立檔案並向檔案寫入資料的方法。通俗地說,檔案處理或檔案管理是指各種過程,例如建立檔案、讀取檔案、寫入檔案、追加檔案等等。檢視和寫入檔案是檔案管理中最常見的兩個操作。

輸入和輸出是由於流導致的,流提供了一個位元組序列的通用檢視。Stream 是一個抽象類。它是不同過程(即輸入和輸出)的閘道器。在 C# 檔案處理中使用檔案流。現在,讓我們討論建立檔案和寫入檔案的不同方法。

1. File.WriteAllText() 方法

這是最常用的方法之一,也是最簡單的方法之一。此方法使用程式設計師定義的名稱建立一個檔案,並寫入來自字串輸入的資料。資料輸入完成後,檔案將關閉。如果使用者要建立的檔案已存在,則儲存中的先前檔案將被覆蓋。

public static void WriteAllText (string path, string? contents); 

兩個輸入引數都是字串。預設情況下,它使用 UTF-8 編碼,沒有 BOM(位元組順序標記)。如果使用者希望使用不同的編碼,則使用者可以為此特定編碼傳遞額外的第三個引數。

演算法

現在,讓我們討論使用 File.WriteAllText() 方法建立檔案並寫入檔案的演算法。

步驟 1 - 宣告一個變數,儲存文字檔名。

步驟 2  宣告一個字串變數,儲存資料。

步驟 3  將資訊輸入檔案並存儲在其中。

步驟 4  寫入資訊後,列印一條成功訊息。

示例

using System.Text;
using System;
using System.IO;
class testfiles {
   public static void Main(){
      var loc = "tutpoint.txt";
      string inform = "Tutorials Point";
      File.WriteAllText(loc, inform);
      
      //The text input is done
      Console.WriteLine("Text input completed.");
   }
} 

輸出

Text input completed. 

2. File.WriteAllLines() 方法

此方法使用程式設計師定義的名稱建立一個檔案,並一次寫入單個字串輸入或多個字串。資料輸入完成後,檔案將關閉。如果使用者要建立的檔案已存在,則儲存中的先前檔案將被覆蓋。

public static void WriteAllLines (string path, string[] contents); 

它使用 UTF-8 編碼,沒有 BOM(位元組順序標記)。

演算法

此演算法是關於 File.WriteAllLines() 的。

步驟 1 - 宣告一個變數,儲存文字檔名。

步驟 2  宣告一個字串變數,儲存資料。

步驟 3  將資料寫入 tutpoint.txt 檔案。

步驟 4  編寫程式碼行以顯示成功完成的工作。

示例

using System.Text;
using System;
using System.IO;
class testfiles {
   public static void Main(){
      var loc = "tutpoint.txt";
      string[] inform = {"Tutorials", "Point", "learn"};
      File.WriteAllLines(loc, inform);
      
      //The text input is done
      Console.WriteLine("Text input completed.");
   }
}

輸出

Text input completed. 

3. File.WriteAllBytes() 方法

如果想進行位元組陣列輸入怎麼辦?好吧,然後我們可以使用 File.WriteAllByttes() 方法。此方法使用程式設計師定義的名稱建立一個檔案。位元組陣列的資料寫入檔案,然後檔案關閉。如果使用者要建立的檔案已存在,則儲存中的先前檔案將被覆蓋。

public static void WriteAllBytes (string path, byte[] bytes); 

演算法

現在,讓我們討論使用 File.WriteAllBytes() 方法建立檔案並寫入檔案的演算法。

步驟 1 - 宣告一個變數,儲存文字檔名。

步驟 2  宣告一個字串變數,儲存資料。

步驟 3  將資訊輸入檔案並存儲在其中。

步驟 4  寫入資訊後,列印一條成功訊息。

示例

using System.Text;
using System;
using System.IO;
class testfiles {
   public static void Main(){
      var loc = "tutpoint.txt";
      string inform = "Tutorial point contains a plethora of technical articles";
      byte[] details = Encoding.ASCII.GetBytes(inform);
      File.WriteAllBytes(loc, details);
      
      //The text input is done
      Console.WriteLine("Text input completed.");
   }
} 

輸出

Text input completed. 

4. 非同步方法

如果使用者希望非同步而不是同步地輸入資料,那麼 c# 也為使用者提供了這種功能。我們上面討論的所有方法也可以非同步使用。在這裡,我們將討論其中一種,其餘方法可以類似地實現。

我們將瞭解 WriteAllTextAsync()。

public static System.Threading.Tasks.Task WriteAllTextAsync (string path, string? contents, System.Threading.CancellationToken cancellationToken = default);

此方法非同步建立檔案,然後將所有文字寫入檔案。之後,檔案將關閉。

演算法

現在,讓我們討論使用 File.WriteAllTextAsync() 方法建立檔案並寫入檔案的演算法。

步驟 1 - 宣告一個變數,儲存文字檔名。

步驟 2  宣告一個字串變數,儲存資料。

步驟 3  將資訊輸入檔案並存儲在其中。

步驟 4  寫入資訊後,列印一條成功訊息。

示例

using System.Text;
using System;
using System.IO;
using System.Threading.Tasks;
class testfiles {
   public static void Main() {
      var loc = "tutpoint.txt";
      string inform = "falcon";
       
      // await File.WriteAllTextAsync(loc, inform);
      Task asyncTask = WriteFileAsync(loc, inform);
        
      //The text input is done
      Console.WriteLine("Text input completed.");
   }
   static async Task WriteFileAsync(string loc, string inform){
      Console.WriteLine("Async Write File has started.");
      using(StreamWriter outputFile = new StreamWriter(Path.Combine(loc)) ){
         await outputFile.WriteAsync(inform);
      }
      Console.WriteLine("Stage 2");
   }
} 

輸出

Async Write File has started.
stage 2
Text input completed.

結論

因此,本文到此結束。在本文中,我們學習了使用 C# 程式建立檔案並向檔案寫入資料的方法。我們學習了各種方法來做到這一點。我們還討論了不同的演算法,並學習了它們的程式碼。我們希望本文能增強您對 C# 的瞭解。

更新於: 2023-03-31

7K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告