为了将字符写入遵循特定编码的流中,我们使用 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
考虑下面的示例来演示如何使用 StreamWriter 将数据写入文件:
代码:
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(); } } }
输出:
在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为check的类。然后调用main方法。然后将文件的路径和文件名分配给一个字符串变量。然后创建字符串编写器类的实例,并将文件的路径作为参数传递以将文本附加到文件。然后包括要附加到文件的数据。然后将数据写入文件后关闭流写入器类的实例。然后以文件的路径为参数从文件中读取数据。
演示 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 "); } } }
输出:
在上面的程序中,定义了一个名为check的类。然后调用main方法。然后创建流写入器类的实例,并将文件的路径作为参数传递给流写入器写入数据。然后使用流编写器类的 write() 方法写入第一行,以便下一行从这里继续。然后使用writeline()方法写入第二行,下一行另起一行。然后使用writeline()方法写入第三行,下一行另起一行。程序的输出如上面的快照所示。
演示 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."); } } }
输出:
在上面的程序中,定义了一个名为check的类。然后调用main方法。然后创建流编写器类的实例,并将必须写入数据的文件的路径作为参数传递。然后定义一个名为plane的变量。然后定义一个称为high的整数。然后使用字符串中的插值语法来提高代码效率。程序的输出如上面的快照所示。
在本教程中,我们通过定义了解 C# 中 StreamWriter 类的概念、C# 中 StreamWriter 类的语法、通过编程示例及其输出了解 StreamWriter 类的工作原理。
以上是C# StreamWriter的详细内容。更多信息请关注PHP中文网其他相关文章!