当前位置: 代码迷 >> C# >> C# IO读写文件步骤封装
  详细解决方案

C# IO读写文件步骤封装

热度:5   发布时间:2016-05-05 03:10:20.0
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;        }

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

  相关解决方案