本文共 825 字,大约阅读时间需要 2 分钟。
使用StreamReader和StreamWriter读写 txt file.//读 public string ReadTxtFile(string path) //ReadAll("A:\\test.txt"); { using (StreamReader sr = new StreamReader(path, Encoding.Default)) { return (sr.ReadToEnd().ToString()); //while ((line = sr.ReadLine()) != null) 一行一行读 //{ // Console.WriteLine(line.ToString()); //} } }//写 public void WriteToTxtFile(string path,string content) { FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sw = new StreamWriter(fs); //写入 sw.Write(content); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); }
转载地址:http://xsmdi.baihongyu.com/