我有一个Web服务器,它将大的二进制文件(几兆字节)读入字节数组。服务器可能同时读取多个文件(不同的页面请求),因此我正在寻找最优化的方式来做到这一点,而又不会给CPU带来太多负担。下面的代码足够好吗?
public byte[] FileToByteArray(string fileName)
{
byte[] buff = null;
FileStream fs = new FileStream(fileName,
FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int) numBytes);
return buff;
}
只需将整个内容替换为:
return File.ReadAllBytes(fileName);
但是,如果您担心内存消耗,你应该不会一下子在所有读取整个文件到内存中。您应该分块进行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句