Ghi các ký tự vào luồng bằng StreamWriter trong C #.
Với StreamWriter, hãy tạo một tệp mới -
StreamWriter sw = new StreamWriter("hello.txt")) Thêm văn bản vào tệp -
sw.WriteLine("Hello");
sw.WriteLine("World"); Sau đây là mã -
Ví dụ
using System.IO;
public class Program {
public static void Main() {
using (StreamWriter sw = new StreamWriter("hello.txt")) {
sw.WriteLine("Hello");
sw.WriteLine("World");
}
}
} Nó tạo tệp “hello.text” và thêm văn bản vào đó -
Đầu ra
Sau đây là kết quả đầu ra.
Hello World