FileStream读写文件

  1 using System;
  2 using System.Data;
  3 using System.IO;
  4 using System.Text;
  5  
  6  /// <summary>
  7  /// Summary description for FileReadAndWrite
  8  /// </summary>
  9  public class FileReadAndWrite
 10  {
 11      public FileReadAndWrite()
 12      {
 13          //
 14          // TODO: Add constructor logic here
 15          //
 16      }
 17      /// <summary>
 18      /// 用FileStream写文件
 19      /// </summary>
 20      /// <param name="str"></param>
 21      /// <returns></returns>
 22      public void FileStreamWriteFile(string str)
 23      {
 24          byte[] byData;
 25          char[] charData;
 26          try
 27          {
 28              FileStream nFile = new FileStream("love.txt", FileMode.Create);
 29              //获得字符数组
 30              charData = str.ToCharArray();
 31              //初始化字节数组
 32              byData = new byte[charData.Length];
 33              //将字符数组转换为正确的字节格式
 34              Encoder enc = Encoding.UTF8.GetEncoder();
 35              enc.GetBytes(charData, 0, charData.Length,byData,0,true);
 36              nFile.Seek(0, SeekOrigin.Begin);
 37              nFile.Write(byData, 0, byData.Length);
 38          }
 39          catch (Exception ex)
 40          {
 41              throw ex;
 42          }
 43      }
 44      /// <summary>
 45      /// FileStream读取文件
 46      /// </summary>
 47      /// <param name="filePath"></param>
 48      /// <returns></returns>
 49      public string FileStreamReadFile(string filePath)
 50      {
 51          byte[] data = new byte[100];
 52          char[] charData = new char[100];
 53          try
 54          {
 55              FileStream file = new FileStream(filePath, FileMode.Open);
 56              //文件指针指向0位置
 57              file.Seek(0, SeekOrigin.Begin);
 58              //读入两百个字节
 59              file.Read(data, 0, 200);
 60              //提取字节数组
 61              Decoder dec = Encoding.UTF8.GetDecoder();
 62              dec.GetChars(data, 0, data.Length, charData, 0);
 63          }
 64          catch (Exception ex)
 65          {
 66              throw ex;
 67          }
 68          return Convert.ToString(charData);
 69      }
 70      /// <summary>
 71     /// StreamWriter写文件
 72     /// </summary>
 73     public void StreamWriterWriteFile()
 74     {
 75         try
 76         {
 77             FileStream nFile = new FileStream("love.txt", FileMode.CreateNew);
 78             StreamWriter writer = new StreamWriter(nFile);
 79 
 80             writer.WriteLine("I love You!");
 81             writer.WriteLine("Do you love me!");
 82             writer.Close();
 83         }
 84         catch
 85         { }
 86     }
 87     /// <summary>
 88     /// StreamReader读取文件
 89     /// </summary>
 90     /// <returns></returns>
 91     public string StreamReaderReadFile()
 92     {
 93         string str="";
 94         try
 95         {
 96             FileStream file = new FileStream("love.txt", FileMode.Open);
 97             StreamReader sr = new StreamReader(file);
 98             while (sr.ReadLine()!=null)
 99             {
100                 str += sr.ReadLine();
101             }
102             //或者str = sr.ReadToEnd();
103             sr.Close();
104         }
105         catch
106         { }
107         return str;
108     }
109 }

 

转载于:https://www.cnblogs.com/besuccess/articles/3476529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值