StreamWriter StreamReader

 private void WriteLoginJsonData(object jsonData)
        {
            using (FileStream writerFileStream = new FileStream(JsonDataFile, FileMode.OpenOrCreate))
            {  
                using (StreamWriter writerStream = new StreamWriter(writerFileStream, Encoding.UTF8))
                {
                    writerFileStream.SetLength(0);
                    string loginDataJson = JsonConvert.SerializeObject(jsonData, Formatting.Indented);
                    writerStream.WriteLine(loginDataJson);
                    writerStream.Close();
                }
                writerFileStream.Close();
            }
        }
private void ReadLoginJsonData()
        {
            if (File.Exists(JsonDataFile))
            {
                using (FileStream readerFileStream = new FileStream(JsonDataFile, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader readerStream = new StreamReader(readerFileStream, Encoding.UTF8))
                    {
                        string jsonContent= readerStream.ReadToEnd();
                        if (!string.IsNullOrWhiteSpace(jsonContent))
                        {                            
                             Console.WriteLine(jsonContent);
                        }
                        readerStream.Close();
                    }
                    readerFileStream.Close();
                }
            }
        }

 

posted @ 2019-10-12 11:15  FredGrit  阅读(301)  评论(0编辑  收藏  举报