C#编程-118:写入文件StreamWriter类

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类C#编程-118:写入文件StreamWriter类C#编程-118:写入文件StreamWriter类C#编程-118:写入文件StreamWriter类C#编程-118:写入文件StreamWriter类

上一篇:《Android 应用案例开发大全(第3版)》——第1.6节本章小结


下一篇:C#编程-117:文件流FileStream类