在C#中如何以5 MiB块的方式读取文件?
FileStream
来读取二进制文件,然后对其进行处理。以下是一个示例: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);
}
}