gpt4 book ai didi

c# - 分块读取大文件c#

转载 作者:可可西里 更新时间:2023-11-01 09:10:57 132 4
gpt4 key购买 nike

我想逐 block 读取非常大的文件 (4GBish)。

我目前正在尝试使用 StreamReaderRead() 读取方法。语法是:

sr.Read(char[] buffer, int index, int count)

因为 index 是一个 int,在我的例子中它会溢出。我应该改用什么?

最佳答案

索引是缓冲区的起始索引而不是文件指针的索引,通常为零。在每次 Read 调用中,您将读取等于 Read 方法的 count 参数的字符。 您不会一次读取所有文件,而是分块读取并使用该 block 。

The index of buffer at which to begin writing, reference.

char[] c = null;
while (sr.Peek() >= 0)
{
c = new char[1024];
sr.Read(c, 0, c.Length);
//The output will look odd, because
//only five characters are read at a time.
Console.WriteLine(c);
}

以上示例将准备好 1024 个字节并将写入控制台。您可以使用这些字节,例如使用 TCP 连接将这些字节发送到其他应用程序。

When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream, where the internal buffer is set to your desired block size, and to always read less than the block size. If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes), MSDN.

关于c# - 分块读取大文件c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21726260/

132 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com