首页  >  文章  >  后端开发  >  C# 中的文本编写器

C# 中的文本编写器

PHPz
PHPz原创
2024-09-03 15:22:45188浏览

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
上一篇:C# BinaryReader下一篇:TextReader in C#