如何在不将整个文件加载到内存中的情况下读取/流式传输文件? [英] How can I read/stream a file without loading the entire file into memory?

查看:34
本文介绍了如何在不将整个文件加载到内存中的情况下读取/流式传输文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不将整个文件加载到内存中的情况下读取任意文件并逐块"处理它(意味着逐字节或其他一些可以提供最佳读取性能的块大小)?处理的一个例子是生成文件的 MD5 哈希,尽管答案可以适用于任何操作.

How can I read an arbitrary file and process it "piece by piece" (meaning byte by byte or some other chunk size that would give the best read performance) without loading the entire file into memory? An example of processing would be to generate an MD5 hash of the file although the answer could apply to any operation.

我想拥有或编写这个,但如果我能得到现有的代码,那也太棒了.

I'd like to have or write this but if I can get existing code that would be great too.

(c#)

推荐答案

这里有一个示例,说明如何在不将整个内容加载到内存中的情况下以 1KB 的块读取文件:

Here's an example of how to read a file in chunks of 1KB without loading the entire contents into memory:

const int chunkSize = 1024; // read the file by chunks of 1KB
using (var file = File.OpenRead("foo.dat"))
{
    int bytesRead;
    var buffer = new byte[chunkSize];
    while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
    {
        // TODO: Process bytesRead number of bytes from the buffer
        // not the entire buffer as the size of the buffer is 1KB
        // whereas the actual number of bytes that are read are 
        // stored in the bytesRead integer.
    }
}

这篇关于如何在不将整个文件加载到内存中的情况下读取/流式传输文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆