C# 属性

C# 抽象

C# 字符串

C# 泛型

C# 杂项

C# 新特性

C# StreamWriter 特定编码将字符写入流

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

C# StreamWriter 示例

让我们看一个 StreamWriter 类的简单示例,它将单行数据写入文件。

例子 (Example)

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

输出:

文件创建成功...

现在打开文件,您将在 output.txt 文件中看到文本“hello c#”。

输出.txt:

你好C#


  • 使用社交账号登录,本站支持
全部评论(0)