返回StreamReader的到开始 [英] Return StreamReader to Beginning
问题描述
我读的行由行的文件,我希望能够重新开始读通过调用一个方法倒带()
。
I'm reading a file in line-by-line and I want to be able to restart the read by calling a method Rewind()
.
如何操作我的就是System.IO.StreamReader
和/或它的基本 System.IO.FileStream
来与读取文件开始?
How can I manipulate my System.IO.StreamReader
and/or its underlying System.IO.FileStream
to start over with reading the file?
我得到了巧妙的主意,用 FileStream.Seek(长,SeekOffset)
走动的文件,但没有任何效果的包围就是System.IO.StreamReader
。我可以关闭()
并重新分配这两个流和读者referecnes,但我希望有一个更好的办法。
I got the clever idea to use FileStream.Seek(long, SeekOffset)
to move around the file, but it has no effect the enclosing System.IO.StreamReader
. I could Close()
and reassign both the stream and the reader referecnes, but I'm hoping there's a better way.
推荐答案
您需要寻求在流,像你一样,然后调用 DiscardBufferedData code>在
的StreamReader
。文档<一href="http://msdn.microsoft.com/en-us/library/system.io.streamreader.discardbuffereddata.aspx">here:
You need to seek on the stream, like you did, then call DiscardBufferedData
on the StreamReader
. Documentation here:
编辑:添加code例如:
Adding code example:
Stream s = new MemoryStream();
StreamReader sr = new StreamReader(s);
// later... after we read stuff
s.Position = 0;
sr.DiscardBufferedData(); // reader now reading from position 0
这篇关于返回StreamReader的到开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!