读取块中的二进制文件 [英] Read Binary File in Chunks
本文介绍了读取块中的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 C# 中以 5 MiB 块读取文件?
How do I read a file in 5 MiB chunks in C#?
推荐答案
您可以使用 FileStream
读取二进制文件,然后对其进行处理.举个例子:
You can use a FileStream
to read a binary file then do your processing on it. Here's a example:
using (var inFileSteam = new FileStream(@"C:\path\to\file.bin", FileMode.Open))
{
byte[] buffer = new byte[5 * 1024 * 1024]; // 5MB in bytes is 5 * 2^20
int bytesRead = inFileSteam.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
for (int i = 0; i < bytesRead; i++)
{
if (buffer[i] == 0x28) // 0010 1000 in binary
{
// replace for example or any other manipulation
buffer[i] = 0x00;
}
}
outFileStream.Write(buffer, 0, bytesRead);
bytesRead = inFileSteam.Read(buffer, 0, buffer.Length);
}
}
这篇关于读取块中的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文