C# 读写文本文件
//读取文件 public string Read(string path) { StreamReader sr=null ; StringBuilder sb = new StringBuilder(); try { sr =new StreamReader(path, Encoding.UTF8); String line; while ((line = sr.ReadLine()) != null) { sb.Append(line + "\r\n"); } } catch (Exception ex) { } finally { //清空缓冲区、关闭流 if (null != sr) { sr.Close(); } } return sb.ToString(); } //读取文件 public void Write(string path, string fileName, string content) { if (!Directory.Exists(path)) Directory.CreateDirectory(path); FileStream fs=null ; try { fs = new FileStream(path + fileName, FileMode.Create); //获得字节数组 byte[] data = System.Text.Encoding.UTF8.GetBytes(content); //开始写入 fs.Write(data, 0, data.Length); } catch(Exception ex) { } finally { //清空缓冲区、关闭流 if (null != fs) { fs.Flush(); fs.Close(); } } }
本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。