C#txt文件读写基本操作

string strFileName=@"C:\Users\Administrator\Desktop\记事2.txt";
            //判断是否存在
            if (File.Exists(strFileName))
            {
                //写文件
                Stream s = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write);
                //打开模式,访问方式
                StreamWriter sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
                sw.Write("111111111111");
                sw.Close();
               
                //追加 1
                StreamWriter wlog;
                wlog = File.AppendText(strFileName);
                wlog.Write("{0}","\r\nsss");
                wlog.Flush();
                wlog.Close();
                //替换
                System.IO.File.WriteAllText(strFileName, string.Empty);
                System.IO.File.WriteAllText(strFileName, "999999999999999");

           }
                //读文件
                Stream s1 = new FileStream(strFileName, FileMode.Open);
                StreamReader sr = new StreamReader(s1, Encoding.Default);
                string sss = sr.ReadToEnd().ToString();
                sr.Close();
                s1.Close();

 

posted @ 2015-09-18 17:45  Mr. Hu  阅读(1622)  评论(0编辑  收藏  举报
Map