文件处理封装
/// <summary>/// 文件處理封裝/// </summary> public static class FileHelper{/// <summary>/// 讀取Txt文件/// </summary>/// <param name="FilePath">文件地址</param>/// <returns>文件內(nèi)容</returns>public static string ReadTxtFile(string FilePath){if (!File.Exists(FilePath)) throw new Exception("指定的文件不存在或已刪除!");return File.ReadAllText(FilePath);}/// <summary>/// 往Txt文件寫入內(nèi)容/// </summary>/// <param name="FilePath"> 文件地址</param>/// <param name="Contents">內(nèi)容</param>/// <param name="IsCover"> 是否覆蓋原內(nèi)容</param>public static void WriteTxtFile(string FilePath, string Contents, bool IsCover){if (!IsCover && File.Exists(FilePath)){using (StreamReader sr = new StreamReader(FilePath)){Contents = sr.ReadToEnd() + " \r\n" + Contents;sr.Close();}}using (StreamWriter sw = new StreamWriter(FilePath)){sw.Write(Contents);sw.Close();}}/// <summary>/// 往Txt文件寫入內(nèi)容/// </summary>/// <param name="FilePath">文件地址</param>/// <param name="Contents">內(nèi)容</param>/// <param name="IsCover">是否覆蓋原內(nèi)容</param>public static void WriteTxtFile(string FilePath, string[] Contents, bool IsCover){string OldContents = string.Empty;if (!IsCover && File.Exists(FilePath)){using (StreamReader sr = new StreamReader(FilePath)){OldContents = sr.ReadToEnd();sr.Close();}}using (StreamWriter sw = new StreamWriter(FilePath)){sw.WriteLine(OldContents);foreach (String str in Contents)sw.WriteLine(str);sw.Close();}}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/AllUserBegin/p/4424177.html
總結(jié)
- 上一篇: 鸿蒙系统公测版发布,鸿蒙OS2.0系统公
- 下一篇: 毫秒级百万数据分页存储过程