Reading large file in chunks c#

扶醉桌前 提交于 2019-11-30 09:13:07

The index is the starting index of buffer not the index of file pointer, usually it would be zero. On each Read call you will read characters equal to the count parameter of Read method. You would not read all the file at once rather read in chunks and use that chunk.

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);
}

The above example will ready 1024 bytes and will write to console. You can use these bytes, for instance sending these bytes to other application using TCP connection.

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.

You could try the simpler version of Read which doesn't chunk the stream but instead reads it character by character. You would have to implement the chunking your self, but it would give you more control allowing you to use a Long instead.

http://msdn.microsoft.com/en-us/library/ath1fht8(v=vs.110).aspx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!