首頁 >後端開發 >C#.Net教程 >C# 中的文字編寫器

C# 中的文字編寫器

PHPz
PHPz原創
2024-09-03 15:22:45308瀏覽

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 在 C# 中如何運作?

要使用 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 的那些衍生類別的清單:

  • IndentedTextWriter: 用於插入製表符字串並追蹤當前的縮排等級。
  • StreamWriter: 用於以特定編碼將字元寫入流。
  • StringWriter: 用於將資訊寫入字串。此資訊儲存在底層 StringBuilder 中。
  • HttpWriter: 它提供了一個 TextWriter 類別的對象,可以透過內部 HttpResponse 物件存取該物件。
  • HtmlTextWriter: 用於將標記字元和文字寫入 ASP.NET 伺服器控制項輸出流。

現在讓我們來討論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.
方法 描述 關閉() 它用於關閉當前編寫器並釋放與該編寫器關聯的所有系統資源。 處置() 用於釋放TextWriter物件所使用的所有資源。 齊平() 它用於清除目前寫入器的所有緩衝區,並使所有緩衝資料寫入底層設備。 同步(TextWriter) 它用於圍繞指定的 TextWriter 創建線程安全的包裝器。 寫入(字元) 用於將字元寫入文字流。 寫入(字串) 用於將字串寫入文字流。 WriteAsync(Char) 用於將字元非同步寫入文字流。 WriteLine() 用於將行終止符寫入文字流。 WriteLineAsync(字串) 用於將字串非同步寫入文字流,後面跟著行終止符。 表>

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:

C# 中的文字編寫器

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:

C# 中的文字編寫器

Conclusion

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn