首页  >  文章  >  后端开发  >  C# StreamWriter

C# StreamWriter

WBOY
WBOY原创
2024-09-03 15:23:32647浏览

为了将字符写入遵循特定编码的流中,我们使用 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

C# 中 StreamWriter 类的工作

  • C# 的文件操作中使用流来从文件中读取数据以及将数据写入文件中。
  • 在应用程序和文件之间创建的额外层称为流。
  • 流使得文件读取顺利,数据写入文件顺利。
  • 来自大文件的数据被分解成小块,然后发送到流。然后,应用程序从流中读取这些数据块,而不是尝试立即读取整个数据。这就是使用流的优点。
  • 文件中的数据被分成小块的原因是,当应用程序尝试一次从文件中读取整个数据时,会对应用程序的性能产生影响。
  • 所以,每当要向文件写入数据时,都会先将数据写入流,然后再将数据从流写入文件。

C# StreamWriter 示例

考虑下面的示例来演示如何使用 StreamWriter 将数据写入文件:

示例#1

代码:

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();
}
}
}

输出:

C# StreamWriter

在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为check的类。然后调用main方法。然后将文件的路径和文件名分配给一个字符串变量。然后创建字符串编写器类的实例,并将文件的路径作为参数传递以将文本附加到文件。然后包括要附加到文件的数据。然后将数据写入文件后关闭流写入器类的实例。然后以文件的路径为参数从文件中读取数据。

示例#2

演示 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 ");
}
}
}

输出:

C# StreamWriter

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后创建流写入器类的实例,并将文件的路径作为参数传递给流写入器写入数据。然后使用流编写器类的 write() 方法写入第一行,以便下一行从这里继续。然后使用writeline()方法写入第二行,下一行另起一行。然后使用writeline()方法写入第三行,下一行另起一行。程序的输出如上面的快照所示。

示例#3

演示 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.");
}
}
}

输出:

C# StreamWriter

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后创建流编写器类的实例,并将必须写入数据的文件的路径作为参数传递。然后定义一个名为plane的变量。然后定义一个称为high的整数。然后使用字符串中的插值语法来提高代码效率。程序的输出如上面的快照所示。

结论

在本教程中,我们通过定义了解 C# 中 StreamWriter 类的概念、C# 中 StreamWriter 类的语法、通过编程示例及其输出了解 StreamWriter 类的工作原理。

以上是C# StreamWriter的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# StreamReader下一篇:C# FileInfo