转:stream分块读取大文本内容

 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();
            }
        }
栋栋
原文地址:https://www.cnblogs.com/zhangdongdong/p/3461141.html