使用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