private void ReadStreamFromFile()
{
string filePath = @"D:\abc.txt";
int bufferSize = 1024000; //每次读取的字节数
byte[] buffer = new byte[bufferSize];
System.IO.FileStream stream = null;
try
{
stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
long fileLength = stream.Length;//文件流的长度
int readCount = (int)Math.Ceiling((double)(bufferSize / fileLength)); //需要对文件读取的次数
int tempCount = 0;//当前已经读取的次数
do
{
stream.Read(buffer, tempCount * bufferSize, bufferSize); //分readCount次读取这个文件流,每次从上次读取的结束位置开始读取bufferSize个字节
//这里加入接收和处理数据的逻辑
//
}
while (tempCount < readCount);
}
catch
{
}
finally
{
if (stream != null)
stream.Dispose();
}
}
超大文本文件 按块读取
最新推荐文章于 2020-12-10 09:15:52 发布