C#程式建立檔案內容字串
簡介
讓我們看看如何建立一個C#程式,從檔案內容建立字串。字串是檔案處理中不可或缺的一部分。C#中的字串是一系列字元。例如,“TutorialsPoint”是一個包含字元't' 'u' 't' 'o' 'r' 'i' 'a' 'l' 's' 'p' 'o' 'i' 'n' 't'的字串。我們使用string關鍵字建立字串。
檔案處理或檔案管理,通俗來說,就是各種流程,例如建立檔案、讀取檔案、寫入檔案、追加檔案等等。檔案的檢視和寫入是檔案管理中最常見的兩個操作。C#中的System.IO類別包含處理輸入和輸出流的類。
由於字串建立是檔案處理的重要組成部分。這裡,完整的文字被讀取,然後轉移到字串中。實際上,有兩種方法可以從檔案內容建立字串。在接下來的部分中,我們將看到兩種讀取檔案內容並將其傳輸到字串中的方法。
1. File.ReadAllText() 方法
這是第一種將檔案所有內容讀取到字串中的方法。這裡,使用了File.ReadAllText()方法。File.ReadAllText()讀取檔案中的所有內容,然後將其傳輸到字串中。檔案的編碼由File.ReadAllText()自動確定。檔案的編碼由其過載版本確定。定義編碼,它是一個編號系統,為字元集中每個書寫字元分配一個數值。字元集可以包含來自字母、數字和其他符號的字元。
在執行開啟檔案的命令時,如果找不到原始檔或發生任何其他型別的I/O錯誤,則會丟擲IOException。如果檔案的輸入和輸出出現任何問題,就會發生這種情況。
演算法
以下演算法將提供使用File.ReadAllText()方法從檔案內容建立字串的分步過程。
例如,如果我們必須讀取檔案中的所有內容,然後將內容傳輸到字串中,那麼我們應該提供其精確的演算法,如下所示:
步驟1 − 建立一個fileName例項以從檔案讀取並向其提供地址。
步驟2 − 使用File.ReadAllText讀取並顯示檔案中的文字行,並將其儲存在text中。
步驟3 − 透過使用catch,我們嘗試捕獲任何發生的錯誤。
步驟4 − 如果有任何錯誤,則將其儲存在e中,然後顯示。
步驟5 − 透過使用Console.Readkey(),我們在結束時暫停程式的執行。
示例
以下是顯示示例的程式碼片段。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Example {
public static void Main() {
string fileName = @"C:\some\path\file.txt";
try {
// Display the lines that are read from the file
string text = File.ReadAllText(fileName);
Console.WriteLine(text);
}
catch (Exception e) {
// Displays the error on the screen.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
輸出
Input is completed
這裡,首先將路徑提供給字串,然後從地址傳遞並開啟該字串。然後,整個內容被複制到建立的字串中。如果檔案無法開啟,則會發生錯誤,並且錯誤訊息顯示在螢幕上。還有另一種方法可以使用SteamReader類。讓我們也看看。
2. SteamReader.ReadToEnd() 方法
File.ReadAllText()的替代方案是SteamReader.ReadToEnd()。它也一次讀取整個檔案,然後將內容複製到字串中。File.OpenText方法由Steam.Reader用於此操作。然後ReadToEnd()方法一次讀取使用者在其中提到的完整檔案。在SteamReader物件的作業完成後,Dispose()方法會自動呼叫,就像Destructor()一樣,這會重新整理/清除流。
演算法
以下演算法將提供使用SteamReader.ReadToEnd()方法從檔案內容建立字串的分步過程。
例如,如果我們必須讀取檔案中的所有內容,然後將內容傳輸到字串中,那麼我們應該提供其精確的演算法,如下所示:
步驟1 − 建立一個fileName例項以從檔案讀取並向其提供地址。
步驟2 − 建立StreamReader例項以讀取檔案內容。
步驟3 − 使用SteamReader.ReadToEnd()讀取檔案中的文字行,並將其儲存在變數text中。
步驟4 − 現在我們使用Console.Writeline()寫入文字資料。
步驟5 − 透過使用Console.Readkey(),我們在結束時暫停程式的執行。
現在,讓我們看看程式碼。
示例
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Example {
public static void Main() {
string fileName = @"C:\some\path\file.txt";
// Creating an instance strRead of StreamReader for reading text from the given file
using (StreamReader strRead = File.OpenText(fileName)) {
string text = strRead.ReadToEnd();
Console.WriteLine(text);
}
Console.ReadKey();
}
}
輸出
Input is completed
當我們使用File.OpenText()時,它預設開啟一個現有的UTF-8編碼檔案。要訪問具有不同字元編碼的檔案,需要使用接受替代字元編碼的StreamReader類建構函式。
給定的示例從具有位元組順序標記識別設定為true的檔案建立一個新的ASCII StreamReader。
示例
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Example {
public static void Main() {
string fileName = @"C:\some\path\file.txt";
// Creating an instance strRead of StreamReader for reading text from the given file
using (StreamReader strRead = new StreamReader(fileName, Encoding.ASCII, true)) {
string text = strRead.ReadToEnd();
Console.WriteLine(text);
}
Console.ReadKey();
}
}
輸出
Input is completed
時間複雜度
在兩個程式碼中,如果我們看到沒有迴圈,因為我們只是建立一個例項來讀取檔案。然後檔案的所有內容都複製到字串中。對於File.ReadAllText()方法,時間複雜度為O(1)。類似地,在SteamReader.ReadToEnd()方法中,時間複雜度為O(1)。
結論
在本文中,我們廣泛討論了從檔案內容建立字串的C#程式。首先,我們討論了字串,然後討論了將檔案完整內容讀取到字串中的不同方法。我們希望本文能幫助您增強對C#的瞭解。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP