- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace StreamWriterTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- string path = @"C:\Users\pengshiyu\Desktop\source\text.txt";
- try
- {
- //way1:
- //FileStream fs = new FileStream(path,FileMode.OpenOrCreate);
- //StreamWriter sw = new StreamWriter(fs);
- //way2:
- StreamWriter sw = new StreamWriter(path, false);
- string mystr = "测试这样话能不能正常显示到文本文件中";
- sw.Write(mystr);
- sw.Close();
- Console.WriteLine("写入完成!");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.ReadKey();
- }
- }
- }
C#编程-118:写入文件StreamWriter类
关注
打赏