C#StreamWriter类用于以特定编码将字符写入流。它继承了TextWriter类。它提供重载的write()和writeln()方法将数据写入文件。
让无涯教程看一个简单的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#”。
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-streamwriter.html
来源:LearnFk无涯教程网
output.txt:
hello c#
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)