你的位置:首页 > ASP.net教程

[ASP.net教程]C# IO读写文件方法封装


  代码 1:

    /// <summary>    /// 功能:FileStream文件流读取文件    /// </summary>    /// <param name="filePath">参数:文件路径</param>    /// <returns>返回值:StreamReader对象</returns>    public static StreamReader ReadFileByFs(string filePath)    {      FileStream Fs = null;      StreamReader Sr = null;      try      {        Fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);        Sr = new StreamReader(Fs, Encoding.Default);      }      catch (IOException e)      {        throw e;      }      return Sr;    }

  代码 2:

    /// <summary>    /// 功能:FileStream文件流写文件    /// </summary>    /// <param name="filePath">参数:文件路径</param>    /// <returns>返回值:StreamWriter对象</returns>    public static StreamWriter WriteFileByFs(string filePath)    {      FileStream Fs = null;      StreamWriter Sw = null;      try      {        Fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);        Sw = new StreamWriter(Fs, Encoding.Default);      }      catch (IOException e)      {        throw e;      }      return Sw;    }

  调用这两个方法读写文件的调用方式我就不写了