欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

StreamWriter StreamReader

程序员文章站 2022-04-15 08:17:45
private void WriteLoginJsonData(object jsonData) { using (FileStream writerFileStream = new FileStream(JsonDataFile, FileMode.OpenOrCreate)) { using..... ......
 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();
                }
            }
        }