我需要处理一个大文件,大约400K行和200M.但有时我必须自下而上处理.我怎样才能在这里使用迭代器(yield return)?基本上我不喜欢在内存中加载所有内容.我知道在.NET中使用迭代器更有效.
在看这个问题时,Jon在回答问题方面做得很好......" 如何用迭代器反向读取文本文件 ".并且有一个类似的问题,我回答使用指针hocus pocus ..'.net有一种方法从它自己关闭之前从下到上读取文本文件 ....
现在我开始尝试使用指针来解决这个问题,好吧,它看起来很粗糙和边缘粗糙......
public class ReadChar : IEnumerable<char>
{
private Stream _strm = null;
private string _str = string.Empty;
public ReadChar(string s)
{
this._str = s;
}
public ReadChar(Stream strm)
{
this._strm = strm;
}
public IEnumerator<char> GetEnumerator()
{
if (this._strm != null && this._strm.CanRead && this._strm.CanSeek)
{
return ReverseReadStream();
}
if (this._str.Length > 0)
{
return ReverseRead();
}
return null;
}
private IEnumerator<char> ReverseReadStream()
{
long lIndex = this._strm.Length;
while (lIndex != …