文件分割、整合

private void togetherFile()//合并文件
{
 string strPath = this.TextBox1.Text;
   StreamReader frlist=new StreamReader(strPath+"."+"list");

 

   FileStream fw = new FileStream(strPath,FileMode.Append, FileAccess.Write);

 

   string sline;

   sline=frlist.ReadLine();

 

   while(sline!=null)

   {

    FileStream fr = new FileStream(sline,FileMode.Open, FileAccess.Read );

 

    byte [] byteread=new byte[fr.Length] ;

    fr.Read(byteread,0,Convert.ToInt32(fr.Length));

 

    foreach(byte bNext in byteread)

     fw.WriteByte(bNext) ;

    fr.Close();

    sline=frlist.ReadLine();

 

   }

   frlist.Close();

   fw.Close();
  }
}

//分割文件
private void cutFile()
{
 int i = 0;
   string strPath = this.TextBox1.Text;
   
   FileStream file = new FileStream(strPath,FileMode.Open);
   BinaryWriter bw = new BinaryWriter(file);
   int FileSize = Convert.ToInt32(file.Length)/10;
   StreamWriter sw = new StreamWriter(strPath+"."+"list",false);
   for(i=1;i<=10;i++)
   {
    byte[] bt = new byte[FileSize];
    file.Read(bt,0,FileSize);
    FileStream fw = new FileStream(strPath+"."+i,FileMode.CreateNew,FileAccess.Write);
    sw.WriteLine(strPath+"."+i);
    foreach(byte btNext in bt)
    {
     fw.WriteByte(btNext);
    }
    fw.Close();
   }
   if(file.Length != file.Position)
   {
    byte [] byteread=new byte[Convert.ToInt32(file.Length) -FileSize*(i-1)] ;

    file.Read(byteread,0,Convert.ToInt32(file.Length) -FileSize*(i-1));

 

    FileStream fw = new FileStream(strPath + "." + i ,FileMode.CreateNew, FileAccess.Write);

 

    sw.WriteLine(strPath +"." + i );

 

    foreach(byte bNext in byteread)

     fw.WriteByte(bNext) ;

    fw.Close();

   }
   sw.Flush();
   file.Close();
   sw.Close();
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值