【发布时间】:2014-02-01 16:10:37
【问题描述】:
我想读取一个大小为数百 GB 甚至 TB 的 CSV 文件。 我有一个限制,我只能读取 32MB 的文件。 我对这个问题的解决方案有点慢,我想问你是否知道更好的解决方案:
const int MAX_BUFFER = 33554432; //32MB
byte[] buffer = new byte[MAX_BUFFER];
int bytesRead;
using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
using (BufferedStream bs = new BufferedStream(fs))
{
string line;
bool stop = false;
while ((bytesRead = bs.Read(buffer, 0, MAX_BUFFER)) != 0) //reading only 32mb chunks at a time
{
var stream = new StreamReader(new MemoryStream(buffer));
while ((line = stream.ReadLine()) != null)
{
//process line
}
}
}
编辑:我添加了一个限制,说我不能逐行读取文件。
【问题讨论】:
-
你试过
File.ReadLines的性能吗? -
@KonradKokosa 您能否在答案中给出解释和示例?老实说,我对文件处理没有那么丰富的经验,所以我不确定 File.ReadLines 和我的处理方式有什么区别
-
顺便说一句:根据您的代码,一行可能会分成两块。
-
@L.B 你是什么意思?
-
没有必要跳过所有这些障碍。有一个StreamReader constructor 可让您指定缓冲区大小。另外,考虑将常量定义为
const int MAX_BUFFER = 32 * 1024 * 1024;这比幻数要清楚得多。顺便说一句,我发现最佳缓冲区大小通常约为 64 KB。更大的缓冲区只会增加不必要的开销,并且通常会使您的程序变慢。