C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# StreamWriter

C# StreamWriter

C# StreamWriter 类用于将字符以特定编码写入流。它继承了 TextWriter 类。它提供了重载的 write() 和 writeln() 方法来将数据写入文件。

C# StreamWriter 示例

让我们看一个简单的 StreamWriter 类示例,它写入一个将单行数据写入文件。
using System;
using System.IO;
public class StreamWriterExample
{
    public static void Main(string[] args)
    {
        FileStream f = new FileStream("e:\\output.txt", FileMode.Create);
        StreamWriter s = new StreamWriter(f);
        s.WriteLine("hello c#");
        s.Close();
        f.Close();
	 Console.WriteLine("File created successfully...");
    }
}
输出:
File created successfully...
现在打开文件,您将在 output.txt 文件中看到文本"hello c#"。
output.txt:
hello c#
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4