TextWriter 用於將文字寫入檔案。以下是關於C#中TextWriter的一些重點,TextWriter是IO命名空間下的抽象類別。它用於將一系列連續的字元寫入檔案中。它是 StreamWriter 和 StringWriter 的基類,分別用於將字元寫入流和字串。
預設情況下,它不是線程安全的。由於它是一個抽象類,因此無法創建它的物件。任何實作 TextWriter 的類別都必須至少實作其 Write(Char) 方法才能創建其有用的實例。
文法及解釋
TextWriter text_writer = File.CreateText(file_path);
如果指定位置(file_path)不存在上述語句,則建立一個新檔案。然後,我們可以使用text_writer來呼叫TextWriter類別的方法,並且可以輕鬆地在C#中處理檔案。
我們可以使用using語句來建立TextWriter,例如:
using(TextWriter text_writer = File.CreateText(file_path)) { //user code }
最好將 TextWriter 與 using 語句一起使用,因為它告訴 .NET 在工作完成且不再需要時釋放 using 區塊中指定的物件。
要使用 TextWriter,首先,我們需要匯入 System.IO 命名空間。現在,我們不能使用「new」關鍵字直接建立 TextWriter 的實例,因為它是一個抽象類別。因此,為了建立實例,我們使用 File 類別的 CreateText() 方法,例如:
TextWriter text_writer = File.CreateText(file_path);
此方法取得要開啟以進行寫入的檔案的路徑。它會建立或開啟一個用於寫入 UTF-8 編碼文字的檔案。如果文件已存在,則其內容將被覆蓋。
它傳回一個StreamWriter的對象,它是TextWriter的衍生類,從而幫助我們建立TextWriter類別的實例。現在,借助該實例,我們可以呼叫 TextWriter 的方法將文字寫入檔案。
TextWriter 是抽象類別 MarshalByRefObject 的衍生類別。其繼承層次如下:
物件——–> MarshalByRefObject ——–>文字編寫器
與 StreamWriter 一樣,還有其他類別衍生自 TextWriter 類,並為 TextWriter 的成員提供實作。請在下面找到我們可以使用 TextWriter 的那些衍生類別的清單:
現在讓我們來討論TextWriter的一些重要方法,例如:
Method | Description |
Close() | It is used to close the current writer and it releases any system resources associated with that writer. |
Dispose() | It is used to release all the resources used by the TextWriter object. |
Flush() | It is used to clear all buffers for the current writer and causes any buffered data to be written to the underlying device. |
Synchronized(TextWriter) | It is used to create a thread-safe wrapper around the specified TextWriter. |
Write(Char) | It is used to write a character to the text stream. |
Write(String) | It is used to write the string to the text stream. |
WriteAsync(Char) | It is used to write the character to the text stream asynchronously. |
WriteLine() | It is used to write line terminator to the text stream. |
WriteLineAsync(String) | It is used to write the string to the text stream asynchronously followed by a line terminator. |
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp2 { class Program { public static void Main() { string file = @"E:\Content\textWriter.txt"; // check if the file exists try { if (File.Exists(file)) { File.Delete(file); } // create the file using (TextWriter writer = File.CreateText(file)) { writer.WriteLine("TextWriter is an abstract class under " + "System.IO namespace. It is used to write sequential " + "series of characters into a file. It is the base class " + "of StreamWriter and StringWriter which is used to " + "write characters to streams and strings respectively. " + "By default, it is not thread safe. " + "As it is an abstract class, its object cannot be created. " + "Any class implementing TextWriter must minimally implement " + "its Write(Char) method to create its useful instance. "); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
Output:
We can asynchronously write characters to stream by using WriteAsync(Char) method such as:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp2 { public class Program { public static void Main(string[] args) { WriteCharAsync(); } public static async void WriteCharAsync() { string file = @"E:\Content\textWriterAsync.txt"; try { //check if file already exists if (File.Exists(file)) { File.Delete(file); } using (StreamWriter writer = File.CreateText(file)) { await writer.WriteLineAsync("TextWriter is an abstract class under "+ "System.IO namespace. It is used to write sequential " + "series of characters into a file. It is the base class " + "of StreamWriter and StringWriter which is used to " + "write characters to streams and strings respectively. " + "By default, it is not thread safe. " + "As it is an abstract class, its object cannot be created. " + "Any class implementing TextWriter must minimally implement " + "its Write(Char) method to create its useful instance. "); await writer.WriteLineAsync("We are writing characters " + "asynchronously."); } } catch(Exception ex) { Console.WriteLine(ex.Message); } } } }
Output:
TextWriter is used to write text or sequential series of characters to a file. A class derived from the TextWriter class needs to provide implementation to any of the members of the TextWriter. Write() methods of TextWriter with primitive data types as parameters write out values as strings.
以上是C# 中的文字編寫器的詳細內容。更多資訊請關注PHP中文網其他相關文章!