我想读一行文本文件中的行。我想知道如果我尽可能有效率地做事情的.NET C#范围之内。
这就是我想到目前为止:
VAR FILESTREAM =新System.IO.FileStream(textFilePath,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite);
var文件=新就是System.IO.StreamReader(FILESTREAM,System.Text.Encoding.UTF8,真实,128);
而((lineOfText = file.ReadLine())!= NULL)
{
//做一些与lineOfText
}
解决方案
如果您使用的是.NET 4中,只需使用的 File.ReadLines
它做这一切为您服务。我怀疑这是的多的和你的一样,但它也可以使用FileOptions.SequentialScan而更大的缓冲区(128看起来非常小的)。
I want to read a text file line by line. I wanted to know if I'm doing it as efficiently as possible within the .NET C# scope of things.
This is what I'm trying so far:
var filestream = new System.IO.FileStream(textFilePath,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite);
var file = new System.IO.StreamReader(filestream, System.Text.Encoding.UTF8, true, 128);
while ((lineOfText = file.ReadLine()) != null)
{
//Do something with the lineOfText
}
解决方案
If you're using .NET 4, simply use File.ReadLines
which does it all for you. I suspect it's much the same as yours, except it may also use FileOptions.SequentialScan
and a larger buffer (128 seems very small).