【发布时间】:2014-10-27 04:28:45
【问题描述】:
我正在尝试根据某个字符的存在来计算大文件的行数,并希望使用 StreamReader 和 ReadBlock - 下面是我的代码。
protected virtual long CalculateRowCount(FileStream inStream, int bufferSize)
{
long rowCount=0;
String line;
inStream.Position = 0;
TextReader reader = new StreamReader(inStream);
char[] block = new char[4096];
const int blockSize = 4096;
int indexer = 0;
int charsRead = 0;
long numberOfLines = 0;
int count = 1;
do
{
charsRead = reader.ReadBlock(block, indexer, block.Length * count);
indexer += blockSize ;
numberOfLines = numberOfLines + string.Join("", block).Split(new string[] { "&ENDE" }, StringSplitOptions.None).Length;
count ++;
} while (charsRead == block.Length);//charsRead !=0
reader.Close();
fileRowCount = rowCount;
return rowCount;
}
但我得到错误
数组的偏移量和长度超出范围或计数大于从索引到源集合末尾的元素数。
我不确定出了什么问题...你能帮忙吗?先谢谢了!
【问题讨论】: