大哥大姐
   我想将一个文件平均分成几分,怎么总是又问题.以下是我的代码(平均分成两分)
                            string selFile="";
string selFile1=@"C:\divide1.txt";
string selFile2=@"C:\divide2.txt";
                           long FileLength=0;
OpenFileDialog fd = new OpenFileDialog();
fd.ShowDialog();
selFile = fd.FileName; FileInfo fin = new FileInfo(selFile);

FileLength = fin.Length; byte []array = new byte[FileLength/2]; MessageBox.Show(selFile + FileLength.ToString()); FileStream fs = new FileStream(selFile,FileMode.Open);
FileStream ss1 = new FileStream(selFile1,FileMode.Create);   
FileStream ss2 = new FileStream(selFile2,FileMode.Create);  fs.Read(array,0,(int)FileLength/2);
ss1.Write(array,0,(int)FileLength/2);
fs.Read(array,(int)FileLength/2,(int)FileLength/2);
                           (就这句)便宜和长度超出数组界线
ss2.Write(array,0,(int)FileLength/2);
fs.Close();
ss1.Close();
ss2.Close();
怎么编译时老师提示越界??给个提示啊??
谁有代码给础一个 啊 ??
谢谢!!!

解决方案 »

  1.   

    fs.Read(array,0,(int)FileLength/2);
    ss1.Write(array,0,(int)FileLength/2);
    fs.Read(array,0,(int)FileLength/2);//第二个参数代表写入array的位置
      

  2.   

    to 象我这样每次都开这么大的数组感觉不太好,有没有更好的实现方法啊!!你能读一半的数据,当然也可以读一部分的数据,道理不都是一样的吗,例如:
    byte []array = new byte[1024];
    int nReadLength = 0;
    long lTotalLength = 0;
    while( lTotalLength < FileLength/2 )
    {
         nReadLength = fs.Read( array,0,array.Length);
         if( nReadLength > 0 )
         {
              ss1.Write(array,0, nReadLength);
              lTotalLength += nReadLength; 
          }
          else
              break;
    }