C#程式:向現有檔案追加文字


簡介

追加是指向已寫入的文件新增資訊。在這裡,我們將學習編寫一個C#程式來向現有檔案追加文字。我們知道C#中可以進行檔案處理。在大多數情況下,檔案用於儲存資料。通俗地說,檔案處理或檔案管理包括各種過程,例如建立檔案、從中讀取、向其中寫入、追加等等。

僅限於現有檔案?

嗯,正如我們所知,追加通常意味著向已寫入的文件新增資訊片段。但是,如果我們嘗試訪問的檔案不存在會怎樣呢?假設我們正在搜尋名為“madrid.txt”的檔案以進行追加。如果該檔案存在於指定的目錄中,則會追加該檔案。但是,如果“madrid.txt”檔案不存在呢?然後,程式將建立一個名為“madrid.txt”的新檔案,您可以在其中新增資訊。因此,當我們嘗試以追加模式開啟檔案時,如果該特定檔案不存在,則會建立一個具有我們想要追加的檔名的新空檔案。

1. File.AppendAllText(String, String) 方法

File.AppendAllText() 方法是我們追加到現有檔案的非常常見的解決方案。此方法來自File類。此方法的語法如下所示。

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

在語法中,第一個字串包含我們打算追加的檔案的路徑。之後,是我們要新增到檔案的資訊。這也會引發一些異常。如果我們嘗試訪問的檔案的目錄不存在,則會引發DirectoryNotFoundException異常。另一個主要的異常是UnauthorizedAccessException異常。當嘗試訪問檔案的程式設計師是一個只讀檔案,或者指定的路徑指向一個目錄而不是檔案時,就會發生這種情況。

使用此方法時,無論是否引發異常,檔案控制代碼都將關閉。

演算法

現在,我們將討論建立一個程式以使用File.AppendAllText()將資訊新增到檔案的演算法。

步驟1 − 首先,我們建立一個字串來儲存要追加的檔案的地址,然後提供檔案的地址。

步驟2 − 然後我們使用FileAppendAllText(),它以追加模式開啟檔案並將特定文字新增到檔案。如果該檔案不存在,則會建立具有該名稱的新檔案並新增文字。

步驟3 − 最後,從檔案中讀取文字,以便我們可以看到檔案已追加,然後程式退出。

示例

// A program to append the file
using System;
using System.IO;

public class Program {
   public static void Main() {
      string loca = @"D:\madrid.txt";

      // Adding the text to the madrid.txt file
      File.AppendAllText(loca, Environment.NewLine + "UCL");

      // Reading the text from the madrid.txt file
      string txtappd = File.ReadAllText(loca);
      Console.WriteLine(txtappd);
   }
}

輸出

UCL

因此,提供了檔案的路徑,然後此方法開啟指定的檔案,新增程式設計師所需的資訊片段,然後關閉檔案。非常簡單,但是如果我們想將整個內容從一個檔案複製到我們想要的檔案怎麼辦?是的,此方法還可以解決我們的檔案複製問題。現在是討論演算法的時候了。

演算法

此演算法是關於使用File.AppendAllText()。

步驟1 − 建立一個字串來儲存原始檔的地址。

步驟2 − 建立另一個字串來儲存目標檔案的地址。

步驟3 − 使用File.Readlines()將原始檔複製到字串中。

步驟4 − 透過File.AppendAllText()以追加模式開啟檔案。然後新增文字。

步驟5 − 完成後程式退出。

示例

// A program to append the file
using System;
using System.Collections.Generic;
using System.IO;

public class Program {
   public static void Main() {
      string sttr1 = @"D:\trophies.txt";
      string sttr2 = @"D:\madrid.txt";

      // Copying all the text from the source file in a string and then adding to the destination file
      IEnumerable<string> inform = File.ReadLines(sttr1);
      File.AppendAllLines(sttr2, inform);
   }
} 

現在讓我們看看另一種方法。

2. File.AppendText() 方法

StreamWriter類是一個非常通用的類。它提供了許多用於向檔案寫入的方法。WriteLine() 或 Write() 是可用於向流新增文字的不同方法。

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

可以使用 File.AppendAllText() 方法建立 StreamWriter 例項,該方法以 UTF-8 編碼將文字追加到現有檔案。如果指定的檔案不存在,它還會建立一個新檔案。

如果我們嘗試訪問的檔案的目錄不存在,則會引發DirectoryNotFoundException異常。另一個主要的異常是UnauthorizedAccessException異常。當嘗試訪問檔案的程式設計師是一個只讀檔案,或者指定的路徑指向一個目錄而不是檔案時,就會發生這種情況。

演算法

現在,我們將討論建立一個程式以使用File.AppendText()將資訊新增到檔案的演算法。

步驟1 − 首先,我們建立一個字串來儲存要追加的檔案的地址,然後提供檔案的地址。

步驟2 − 現在,我們建立一個StreamReader例項。此步驟以追加模式開啟檔案並將文字新增到檔案。我們使用File.AppendText()新增文字。

StreamReader.Write() 方法用於追加。如果使用者想要追加文字並在末尾新增行終止符。使用 StreamReader.WriteLine() 方法。

步驟3 − 完成後程式退出。

示例

// A program to append the file
using System;
using System.IO;

public class Program {
   public static void Main() {
      string loca = @"D:\madrid.txt";

      // Adding the text to the specified file
      using (StreamWriter sw = File.AppendText(loca)) {
         sw.Write("UCL"); //use sw.WriteLine(If you want to add line termination)
      }

      // Read the text from the appended file
      string txtappd = File.ReadAllText(loca);
      Console.WriteLine(txtappd);
   }
}

輸出

UCL

StreamWriter(String, Boolean) 建構函式的過載版本也等效於 File.AppendText()。對於布林引數,我們使用 true。

StreamWriter(String, Boolean)演算法

現在,我們將討論建立一個程式以使用StreamWriter(String, Boolean)將資訊新增到檔案的演算法。

步驟1 − 首先,我們建立一個字串來儲存要追加的檔案的地址,然後提供檔案的地址

步驟2 − 現在,我們建立一個StreamReader例項。此步驟以追加模式開啟檔案並將文字新增到檔案。我們使用新的 Streamwriter() 新增資訊。在這裡,我們使用 StreamReader.Write() 方法進行追加。但是,如果我們需要追加文字並在末尾新增行終止符,則可以使用 StreamReader.WriteLine() 方法。

步驟3 − 最後,從檔案中讀取文字,以便我們可以看到檔案已追加,然後程式退出。

示例

// A program to append the file
using System;
using System.IO;

public class Program {
   public static void Main() {
      string loca = @"D:\madrid.txt";

      // Adding the text to the specified file
      using (StreamWriter sw = new StreamWriter(loca, true)) {
         sw.Write("UCL"); //use sw.WriteLine(If you want to add line termination)
      }

      // Read the text from the appended file
      string txtappd = File.ReadAllText(loca);
      Console.WriteLine(txtappd);
   } 
}

輸出

UCL

時間複雜度

由於在這裡在這兩個過程中,我們都使用檔案處理。在第一個演算法中,我們使用File.AppendAllText(),在第二個演算法中,我們使用了File.AppendText(),並且兩者都只是追加檔案。它們獲取新文字並將其新增到檔案的末尾。因此,這裡兩種方法的時間複雜度都是O(1)。

結論

在本文中,我們討論了向現有檔案追加文字的不同方法。首先,我們討論了需求以及是否只能對現有檔案執行此操作。然後,我們討論了追加File.AppendAllText()和File.AppendText()的方法。最後,我們討論了演算法和演算法的程式碼。

我們希望本文能幫助您提高對C#的瞭解。

更新於:2023年7月14日

9000+ 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.