FileStream读/写方法的限制
FileStream 的读/写方法只能采用整数值作为长度。但是FileStream
对象返回long
中的长度。在这种情况下,如果文件大小大于整数值(大约超过 2GB)怎么办?那么FileStream的读/写方法如何处理long
值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然后,您可以分多个块进行读取和写入。无论如何,CLR 对任何特定对象的大小都有限制(即使在 64 位 CLR 上,也大约是 2GB IIRC),因此您不能拥有足够大的字节数组以使其成为问题。
无论如何,您应该在读取时始终循环,因为您无法保证 Read 调用将读取您请求的字节数,即使有更多数据即将到来。
编辑:分块阅读:
分块写作将取决于你正在写的内容......很难抽象地谈论它。
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.
EDIT: Reading in chunks:
Writing in chunks will depend on what you're writing... it's hard to talk about it in the abstract.
无需在一次调用中直接写入超过 2Gb 的数据
如果您确实在内存中连续缓冲了这么多数据(?可能作为不安全获取的 UnmanagedMemoryStream 来实现核心转储?),您可以轻松地在多个调用中批量写入。无论如何,在当前硬件上,它将以 512k 到最大 4k 的块写入磁盘。
“流”界面的巨大价值在于您可以通过任何方式获得它。事实上,当您查看它时,您会发现 CLR 数组(以及其他任何内容)实际上限制为 2GB
更新,
因为您现在已经承认您基本上想要复制流,那么即时解决方案可能会更好。有
File.Copy
或者有 标准答案
不要忘记
刷新
、关闭
和处置
您的流如果您手动处理流,则合适。干杯
There is no need to directly write more than 2Gb of data in one call
If you would really have that amount buffered contiguously in memory (? maby as an unsafely acquired UnmanagedMemoryStream to implement a core dump?) you could easily batch the writes in multiple calls. It will get written to disk in blocks of 512k to max 4k on current hardware, anyway.
The great value of 'streaming' interfaces is that you can have it anywhich way. In fact, when you look into to it you will find that the CLR Arrays (and anything else) are actually bounded to 2GB
Update
Since you have now confessed that you basically want to copy streams, you might be better served with an instant solution. There is
File.Copy
Or there is the standard answer
Don't forget about
Flushing
,Closing
andDisposing
your Streams as appropriate if you are handling the streams manually.Cheers
据我所知,您仍然可以使用查找来到达流中的正确位置。
你可能需要循环才能做到这一点,如果你想阅读超过 2 场演出,你也需要在这里循环
as far as i know you can still use seek to get to the right position in the stream.
you will probably have to loop to do so, and if you want to read more than 2 gigs wou will need to loop here too