本文介绍了在哪里使用 StreamReader.DiscardBufferedData()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
StreamReader StreamReader = new StreamReader("File.txt");
string Line = "";
while((Line = StreamReader.ReadLine()) != null)
{
string Value = Line.Trim();
string Value2 = StreamReader.ReadLine();
int CursorValue = StreamReader.BaseStream.Position;
if(Condition)
{
StreamReader.BaseStream.Position = SOMETHING_ELSE;
Console.WriteLine(StreamReader.ReadLine());
}
StreamReader.BaseStream.Position = CursorValue;
}
有人能告诉我应该在哪里使用 StreamReader.DiscardBufferedData()
吗?
Can anybody tell me where I am supposed to use StreamReader.DiscardBufferedData()
here?
推荐答案
来自 MSDN:
使用 DiscardBufferedData 方法重置 StreamReader 对象的内部缓冲区.只有在内部缓冲区和BaseStream的位置不匹配时才需要调用该方法.
举个例子:
Stream s = new MemoryStream();
StreamReader sr = new StreamReader(s);
// Read things here.
s.Position = 10;
sr.DiscardBufferedData(); // reader now reading from position 0
sr.BaseStream.Seek(10, SeekOrigin.Begin); // Reader now reading from location 10.
就你而言:
StreamReader.BaseStream.Position = SOMETHING_ELSE;
sr.DiscardBufferedData();
StreamReader.BaseStream.Seek(SOMETHING_ELSE, SeekOrigin.Begin);
Console.WriteLine(StreamReader.ReadLine());
不要忘记再次回到:
StreamReader.BaseStream.Position = CursorValue;
sr.DiscardBufferedData();
StreamReader.BaseStream.Seek(CursorValue, SeekOrigin.Begin);
这篇关于在哪里使用 StreamReader.DiscardBufferedData()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!