如下代码:
            FileStream fs = new FileStream("c:\\1.mp3", FileMode.Open);
            byte[] tmpbyte = new byte[fs.Length ];
            fs.Read(tmpbyte, 0, fs.Length);
            fs.Close ()
因为这个文件的长度已经超过了Read方法中第三个参数int类型的大小,所以会报错。
不知道有什么好办法来处理呢?

解决方案 »

  1.   

    将文件分为几节读取,然后在内存中使用long类型数组合并,最后再转为Stream。。
      

  2.   

    数组的最大长度是Int32.MaxValue
    所以你可以申请一个交叉数组
    byte[][] b = new byte[fs.Length/In32.MaxValue+1][];
    然后循环读取
    int i;
    for(i = 0;i<fs.Length/Int32.MaxValue;i++)
    {
    b[i][] = new Byte[int.MaxValue];
    fs.Read(b[i], i*int.MaxValue, int.MaxValue);
    }
    把剩余的再读一下
    fs.Read(b[i], i*int.MaxValue, fs.Length-i*int.MaxValue);
      

  3.   

    数组的最大长度是Int32.MaxValue
    所以你可以申请一个交叉数组
    byte[][] b = new byte[fs.Length/In32.MaxValue+1][];
    然后循环读取
    int i;
    for(i = 0;i<fs.Length/Int32.MaxValue;i++)
    {
    b[i][] = new Byte[int.MaxValue];
    fs.Read(b[i], i*int.MaxValue, int.MaxValue);
    }
    把剩余的再读一下
    b[i] = new Byte[fs.Length-i*int.MaxValue]
    fs.Read(b[i], i*int.MaxValue, fs.Length-i*int.MaxValue);
      

  4.   

    FileStream fs = new FileStream("c:\\1.mp3", FileMode.Open);
    byte[] tmpbyte = new byte[fs.Length ];
    int count=fs.Read(tmpbyte,0,1024);
    int tmpIndex=0;
    while(count!=0)
    {
    tmpIndex=(tmpIndex+1)*1024;
    fs.Read(tmpbyte,tmpIndex , count);
    }
    fs.Close ()
      

  5.   

    int32的最大值大约21亿,也就是2.1G。
    你要将这么大的文件完全读到内存,程序还能跑得动吗?
    不知道你想干什么,换个别的解决思路吧。
      

  6.   

    FileStream fs = new FileStream("c:\\1.mp3", FileMode.Open);
    byte[] tmpbyte = new byte[fs.Length ];
    int count=fs.Read(tmpbyte,0,1024);
    int tmpIndex=0;
    while(count!=0)
    {
    tmpIndex=(tmpIndex+1)*1024;
    fs.Read(tmpbyte,tmpIndex , count);
    }
    fs.Close ()