IO流操作读写

/// <summary>
        /// 记录bug,以便调试
        /// </summary>
        /// <returns></returns>
        public bool WriteTxt(string str)
        {
            try
            {
                FileStream fs = new FileStream(Server.MapPath("/bugLog.txt"), FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
向文件新增一行


 public string ReaderTxt()
        {
            string sLine = "";
            try
            {
                StreamReader objReader = new StreamReader(Server.MapPath("/bugLog.txt"));
                sLine = objReader.ReadToEnd();
                objReader.Close();
            }
            catch (Exception)
            {
                sLine = "读取失败";
            }
            return sLine;
        }


你可能感兴趣的:(IO流操作读写)