的FileStream读取/写入方法的局限性 [英] FileStream Read/Write method's limitation
问题描述
的FileStream的读取/写入方法只能取整数
值长度。在但
。在这种情况下,如果文件大小是比的FileStream
对象返回长度长整数
值(大约超过2GB)大。那么怎样的FileStream的读取/写入方法句柄长
值。
FileStream's read/write method can take only integer
value as length. But FileStream
object returns length in long
. In this case, what if file size is larger than integer
value (approximate more than 2GB). Then how FileStream's read/write method handle long
value.
推荐答案
然后你看,多写大块英寸在CLR对任何特定的对象的大小有限制呢(也2GB左右IIRC,即使是在64位CLR),所以你不能有一个字节数组足够大,它是一个问题。
Then you read and write in multiple chunks. The CLR has a limit on the size of any particular object anyway (also around 2GB IIRC, even on a 64-bit CLR), so you couldn't have a byte array big enough for it to be a problem.
您应该始终循环读取反正时候,因为你不能保证一个读通话将读取的字节数,你的要求,即使有更多的数据来。
You should always loop when reading anyway, as you can't guarantee that a Read call will read as many bytes as you requested, even if there's more data to come.
编辑:读取中块:
byte[] buffer = new byte[1024 * 32];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
// Use the data you've read
}
在块将取决于你写什么
写作......很难讲关于它的抽象。
Writing in chunks will depend on what you're writing... it's hard to talk about it in the abstract.
这篇关于的FileStream读取/写入方法的局限性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!