这篇文章主要用C#技术讲解了IO读写文件方法封装实例,有需要的朋友可以参考下
具体不做详细介绍了,直接上代码
- /// <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;
- }
以上代码针对IO读写文件方法封装做了详细介绍,希望能够帮助到大家。
新闻热点
疑难解答