filestream读取大文件的问题

myth_sky 2010-05-07 06:52:24
如下代码:
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类型的大小,所以会报错。
不知道有什么好办法来处理呢?
...全文
534 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
oneatree 2011-01-10
  • 打赏
  • 举报
回复
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 ()
wangwenzhuang 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 computerfox 的回复:]
引用 8 楼 justindreams 的回复:

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)
{……
[/Quote]
这个读小文件还可以,大文件,会内存溢出的!
wangwenzhuang 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 computerfox 的回复:]
引用 8 楼 justindreams 的回复:

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)
{……
[/Quote]
这个读小文件还可以,大文件,会内存溢出的!
zzx509 2010-05-07
  • 打赏
  • 举报
回复
int32的最大值大约21亿,也就是2.1G。
你要将这么大的文件完全读到内存,程序还能跑得动吗?
不知道你想干什么,换个别的解决思路吧。
捷哥1999 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 justindreams 的回复:]

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)*102……
[/Quote]

这个方法可以,循环读取。
justindreams 2010-05-07
  • 打赏
  • 举报
回复
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 ()
chowyi 2010-05-07
  • 打赏
  • 举报
回复
交叉数组
liuyileneal 2010-05-07
  • 打赏
  • 举报
回复
数组的最大长度是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);
足球中国 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liuyileneal 的回复:]
数组的最大长度是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]……
[/Quote]
不错。
liuyileneal 2010-05-07
  • 打赏
  • 举报
回复
数组的最大长度是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);
njw1028 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lpingz 的回复:]
分段读取,每次读1024 ,对是否读取到1024进行判断,是则继续,否则已到结尾。
[/Quote]
ABCD
lpingz 2010-05-07
  • 打赏
  • 举报
回复
分段读取,每次读1024 ,对是否读取到1024进行判断,是则继续,否则已到结尾。
bai_bing_wei 2010-05-07
  • 打赏
  • 举报
回复
将文件分为几节读取,然后在内存中使用long类型数组合并,最后再转为Stream。。
myth_sky 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liuyileneal 的回复:]
数组的最大长度是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]……
[/Quote]

寒,交叉数组没有用过。。去查下资料先。
myth_sky 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zzx509 的回复:]
int32的最大值大约21亿,也就是2.1G。
你要将这么大的文件完全读到内存,程序还能跑得动吗?
不知道你想干什么,换个别的解决思路吧。
[/Quote]

嗯。。我也这么想。。换种解决办法

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧