大文本文件的读取与写入

string   strInput=@"c:/file.input";  
  string   strOutput=@"c:/file.input";  
   
  public   void   ReadLargeFile()  
  {  
  byte[]   btsReadBuffer=new   byte[1024*8];  
   
  //异步开始读取文件内容  
  FileStream     fsRead=new   FileStream(strInput,FileMode.Open,FileAccess.Read,FileShare.Write);  
  if(fsRead.CanRead)  
  {  
  fsRead.BeginRead(btsReadBuffer,0,btsReadBuffer.Length,new   AsyncCallback(Callback_ReadFile),fsRead);  
  }  
  else  
  {  
  fsRead.Close();  
  throw   new   ApplicationException("Current   Stream   Couldn't   Read!");  
  }  
  }  
   
  ///   <summary>  
  ///   异步读取文件后所进行的操作  
  ///   </summary>  
  ///   <param   name="ar"></param>  
  public   void   Callback_ReadFile(IAsyncResult   ar)  
  {  
  FileStream   fsRead=null;  
  FileStream   fsWrite=null;  
   
  byte[]   btsWriteBuffer=new   byte[1024*8];  
  fsRead=(FileStream)ar.AsyncState;  
  int   intReadCount=fsRead.EndRead(ar);  
  if(intReadCount>0)  
  {  
  string   strReadContent=Encoding.Default.GetString(btsReadBuffer,0,btsReadBuffer.Length);  
  //这里放处理代码  
  //.....  
  //....  
   
  btsWriteBuffer=Encoding.Default.GetBytes(strReadContent);  
  fsWrite=new   FileStream(strOutput,FileMode.OpenOrCreate,FileAccess.Write,FileShare.Read);  
  if(fsWrite.CanWrite)  
  {  
  fsWrite.BeginWrite(btsWriteBuffer,0,btsWriteBuffer.Length,new   AsyncCallback(Callback_WriteFile),fsWrite);  
  }  
  else  
  {  
  fsWrite.Close();  
  throw   new   ApplicationException("Current   Stream   Couldn't   Write!");  
  }  
  }  
  else  
  {  
  fsRead.Close();  
  return;  
  }  
  fsRead.BeginRead(tbp1_GlobalData.btsBuffer,0,tbp1_GlobalData.btsBuffer.Length,new   AsyncCallback(Callback_ReadFile),fsRead);  
  }  
   
   
  public   void   Callback_WriteFile(IAsyncResult   ar)  
  {  
  FileStream   fsWrite=null;  
  try  
  {  
  fsWrite=(FileStream)ar.AsyncState;  
  fsWrite.EndWrite(ar);  
  fsWrite.Close();  
  }  
  catch(Exception   ex)  
  {  
  fsWrite.Close();  
  throw   ex;  
  }  
  }   
    
  http://topic.csdn.net/t/20061231/23/5269461.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值