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中文网其他相关文章!