将序列化的json字符串内容写入Json 记事本文件,并且保存


/// <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

  


posted @ 2022-11-30 09:13  LowKeyC  阅读(212)  评论(0编辑  收藏  举报
有志者事竟成破釜沉舟百二秦关终属楚苦心人,天不负,卧薪尝胆,三千越甲可吞吴