zoukankan      html  css  js  c++  java
  • 文件操作

    protected void Write_Txt(string FileName, string Content)
           {
               Encoding code = Encoding.GetEncoding("gb2312");
               string htmlfilename = HttpContext.Current.Server.MapPath("Precious\" + FileName + ".txt"); //保存文件的路径
               string str = Content;
               StreamWriter sw = null;
               {
                   try
                   {
                       sw = new StreamWriter(htmlfilename, false, code);
                       sw.Write(str);
                       sw.Flush();
                   }
                   catch { }
               }
               sw.Close();
               sw.Dispose();
     
           }
    protected string Read_Txt(string filename)
            {
     
                Encoding code = Encoding.GetEncoding("gb2312");
                string temp = HttpContext.Current.Server.MapPath("Precious\" + filename + ".txt");
                string str = "";
                if (File.Exists(temp))
                {
                    StreamReader sr = null;
                    try
                    {
                        sr = new StreamReader(temp, code);
                        str = sr.ReadToEnd(); // 读取文件
                    }
                    catch { }
                    sr.Close();
                    sr.Dispose();
                }
                else
                {
                    str = "";
                }
                return str;
            }
    #region 写文件
          /****************************************
           * 函数名称:WriteFile
           * 功能说明:当文件不存时,则创建文件,并追加文件
           * 参    数:Path:文件路径,Strings:文本内容
           * 调用示列:
           *           string Path = Server.MapPath("Default2.aspx");       
           *           string Strings = "这是我写的内容啊";
           *           DotNet.Utilities.FileOperate.WriteFile(Path,Strings);
          *****************************************/
          /// <summary>
          /// 写文件
          /// </summary>
          /// <param name="Path">文件路径</param>
          /// <param name="Strings">文件内容</param>
          public static void WriteFile(string Path, string Strings)
          {
     
              if (!System.IO.File.Exists(Path))
              {
                  System.IO.FileStream f = System.IO.File.Create(Path);
                  f.Close();
                  f.Dispose();
              }
              System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
              f2.WriteLine(Strings);
              f2.Close();
              f2.Dispose();
     
     
          }
          #endregion
     
          #region 读文件
          /****************************************
           * 函数名称:ReadFile
           * 功能说明:读取文本内容
           * 参    数:Path:文件路径
           * 调用示列:
           *           string Path = Server.MapPath("Default2.aspx");       
           *           string s = DotNet.Utilities.FileOperate.ReadFile(Path);
          *****************************************/
          /// <summary>
          /// 读文件
          /// </summary>
          /// <param name="Path">文件路径</param>
          /// <returns></returns>
          public static string ReadFile(string Path)
          {
              string s = "";
              if (!System.IO.File.Exists(Path))
                  s = "不存在相应的目录";
              else
              {
                  StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
                  s = f2.ReadToEnd();
                  f2.Close();
                  f2.Dispose();
              }
     
              return s;
          }
          #endregion
  • 相关阅读:
    解决部分小程序无法获取UnionId的问题
    你也可以写个聊天程序
    JavaScript 数据结构与算法之美
    CSS content应用
    JS中判断null、undefined与NaN的方法
    IT资料常用网址汇总
    史上最全的正则表达式-匹配中英文、字母和数字
    百万数据修改索引,百万数据修改主键
    SQL Server 2005 实现数据库同步备份 过程--结果---分析
    数据库性能优化三:程序操作优化
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/7474485.html
Copyright © 2011-2022 走看看