使用StreamReader与StreamWriter进行文本文件读写

时间:2022-09-22 19:10:56
namespace filetest
{
    class FileUtil
    {
        public static void WriteFile(string file)
        {
            using (FileStream fs = new FileStream(file,FileMode.Append))
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine("Hello,this is a StreamWriter test!");
            }
            

        }

        public static void ReadFile(string file)
        {
            StringBuilder sb = new StringBuilder();
            using(FileStream fs = new FileStream(file, FileMode.Open))
            using(StreamReader sr = new StreamReader(fs))
            {
                while (!sr.EndOfStream)
                {
                    sb.Append(sr.ReadLine());
                }
            }
            Console.WriteLine(sb.ToString());
        }
    }
}