/// <summary> /// 将序列化的json字符串内容写入Json文件,并且保存 /// </summary> /// <param name="path">路径</param> /// <param name="jsonConents">Json内容</param> private static void WriteJsonFile(string path, string jsonConents) { using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite)) { fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { sw.WriteLine(jsonConents); } } } /// <summary> /// 获取到本地的Json文件并且解析返回对应的json字符串 /// </summary> /// <param name="filepath">文件路径</param> /// <returns>Json内容</returns> private string GetJsonFile(string filepath) { string json = string.Empty; using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) { json = sr.ReadToEnd().ToString(); } } return json; } ———————————————— 版权声明:本文为CSDN博主「熊思宇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_38693757/article/details/115719324
标签:fs,string,FileStream,json,Json,new,序列化 From: https://www.cnblogs.com/LowKeyCXY/p/16937406.html