我怎么能读/流的文件而不需要将整个文件加载到内存中? [英] How can I read/stream a file without loading the entire file into memory?

查看:866
本文介绍了我怎么能读/流的文件而不需要将整个文件加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能不看整个文件加载到内存中的任意文件,并对其进行处理一块一块(即按字节或一些其他的块大小,将给予最好的读取性能字节)?处理的一个实例是将生成的文件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.

我想有或写这个,但如果我能得到现有code,这将是巨大的。

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天全站免登陆