為了將字元寫入遵循特定編碼的流中,我們使用 C# 中稱為 StreamWriter 類別的類,StreamWriter 類別的 StreamWriter.Write() 方法負責將字元寫入流中。 TextWriter類別是StreamWriter類別的基類,StreamWriter類別繼承自TextWriter類,而TextWriter類別提供了多種方法,可用於將物件寫入字串、將字串寫入檔案、序列化XML等. 而System.IO .namespace 是定義StreamWriter 的命名空間,StreamWriter 類別提供了Write、WriteAsync、WriteLine、WriteLineAsync 等多種Write 方法
C#中StreamWriter類別的語法如下:
public class StreamWriter : System.IO.TextWriter
考慮以下範例來示範如何使用 StreamWriter 將資料寫入檔案:
代碼:
using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; //a namespace called program is defined namespace program { //a class called check is defined class check { //main method is called static void Main(string[] args) { //the path of the file and the file name is assigned to a string variable String pat = @"D:\Ex.txt"; //an instance of the string writer class is created, and the path of the file is passed as a parameter to append text to the file using (StreamWriter sw = File.AppendText(pat)) { //data to be appended to the file is included sw.WriteLine("Welcome to StreamWriter class in C#"); //the instance of the streamwriter class is closed after writing data to the File sw.Close(); //data is read from the file by taking the path of the file as parameter Console.WriteLine(File.ReadAllText(pat)); } Console.ReadKey(); } } }
輸出:
在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為check的類別。然後呼叫main方法。然後將檔案的路徑和檔案名稱指派給一個字串變數。然後建立字串編寫器類別的實例,並將檔案的路徑作為參數傳遞以將文字附加到檔案中。然後包括要附加到文件的資料。然後將資料寫入檔案後關閉流寫入器類別的實例。然後以文件的路徑為參數從文件中讀取資料。
示範 StreamWriter 類別用法的程式:
代碼:
using System.IO; //a class called check is defined class check { //main method is called static void Main() { //an instance of streamwriter class is created and the path of the file is passed as a parameter using (StreamWriter sw = new StreamWriter(@"D:\imp.txt")) { //write() method of stream writer class is used to write the first line so that the next line continues from here sw.Write("Welcome to StreamWriter class in C# and "); //writeline() method is used to write the second line and the next line starts from a new line sw.WriteLine("this program is demonstration of StreamWriter class in C# "); //writeline() method is used to write the third line and the next line starts from a new line sw.WriteLine("I hope you are learning "); } } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後建立流寫入器類別的實例,並將檔案的路徑作為參數傳遞給流寫入器寫入資料。然後使用流編寫器類別的 write() 方法寫入第一行,以便下一行從這裡繼續。然後使用writeline()方法寫入第二行,下一行另起一行。然後使用writeline()方法寫入第三行,下一行另起一行。程式的輸出如上面的快照所示。
示範 StreamWriter 類別用法的程式:
代碼:
using System.IO; //a class called check is defined class check { //main method is called static void Main() { //an instance of the stream writer class is created and the path of the file to which the data must be written is passed as a parameter using (StreamWriter sw = new StreamWriter(@"D:\Ex.txt")) { //a variable called plane is defined string plane = "Tejas"; //an integer called high is defined int high = 120; //interpolation syntax in string is used to make code efficient. sw.WriteLine($"The plane {plane} flies {high} feet high."); } } }
輸出:
在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後建立流編寫器類別的實例,並將必須寫入資料的檔案的路徑作為參數傳遞。然後定義一個名為plane的變數。然後定義一個稱為high的整數。然後使用字串中的插值語法來提高程式碼效率。程式的輸出如上面的快照所示。
在本教程中,我們透過定義來了解 C# 中 StreamWriter 類別的概念、C# 中 StreamWriter 類別的語法、透過程式設計範例及其輸出來了解 StreamWriter 類別的工作原理。
以上是C# StreamWriter的詳細內容。更多資訊請關注PHP中文網其他相關文章!