C# 中的 StreamWriter

csharpprogrammingserver side programming更新于 2025/9/8 17:37:17

使用 C# 中的 StreamWriter 将字符写入流。

使用 StreamWriter 创建一个新文件 −

StreamWriter sw = new StreamWriter("hello.txt"))

向文件添加文本 −

sw.WriteLine("Hello");
sw.WriteLine("World");

以下是代码 −

示例

using System.IO;

public class Program {
   public static void Main() {
      using (StreamWriter sw = new StreamWriter("hello.txt")) {
         sw.WriteLine("Hello");
         sw.WriteLine("World");
      }
   }
}

创建文件 “hello.text” 并向其中添加文本 −

输出

以下是输出。

Hello
World

相关文章