什么是阅读文本文件中的行由行的最快的方法是什么? [英] What's the fastest way to read a text file line-by-line?

查看:219
本文介绍了什么是阅读文本文件中的行由行的最快的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读一行文本文件中的行。我想知道如果我尽可能有效率地做事情的.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 它做这一切为您服务。我怀疑这是的的和你的一样,但它也可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.io.fileoptions.aspx"><$c$c>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).

这篇关于什么是阅读文本文件中的行由行的最快的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆