• 转: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();
                }
            }
    栋栋
  • 相关阅读:
    ubuntu开机启动
    我的linux之路
    继承
    oracle 10g win7 win8 下的安装
    Java 安全检测
    BigDecimal 四舍五入
    Java Map 便利
    java split . |
    java 爬虫 Jsoup
    获取当前路径
  • 原文地址:https://www.cnblogs.com/zhangdongdong/p/3461141.html
Copyright © 2020-2023  润新知