C#读取超大文本文件

现有一个超大TXT文件,内部以\t分格,每行244个值,一共250万行,大小1.9G,第一行是列名,现在要将每一行的数据读出来进行处理,如果一次性读进内存肯定是不行的。

目录

读第一行:

读后续行:

遍历方法

使用感受

参考:


读第一行:


 string headerLine = File.ReadLines(path).FirstOrDefault(); // Read the first row for headings

读后续行:

_filePath是文件路径

_currentLine是读过的行数

limit指定的是一次读多少行

_encoding指定编码为GBK或UTF-8

static private System.Text.Encoding _encoding = Encoding.GetEncoding("gb2312");//Encoding.UTF8; //Encoding.ASCII;

       public static IEnumerable<string>  readMultiLines(int limit = 100)
        {
            try
            {
                var lines = File.ReadLines(_filePath, _encoding).Skip(_currentLine).Take(limit);
                _currentLine += limit;
                return lines;
            }
            catch(Exception  ){
                return null;  
            }
        }

遍历方法

ReadLines返回的是一个IEnumerable<string>  类型,可以用iter进行遍历。

iter.Current直接获取每一行的字符串,不可以用iter.toString(),会很费时间。

   var lines = File.ReadLines(_filePath, _encoding).Skip(_currentLine).Take(bigStep);
   var iter = lines.GetEnumerator();
   int size = lines.Count<string>();

   while (iter.MoveNext())
   {
       string dataStr = iter.Current;
       fun(dataStr);         
                   
    } 

使用感受

每次读500行,在读取前100万行时感觉还非常快,但在后面感觉还是有一点的延时。不过应该满足需求了。

 

 

参考:

1.https://github.com/Agenty/FileReader/blob/master/FileReader/Core/Extension.cs

2.https://blog.csdn.net/callmeback/article/details/8295648

3.https://stackoverflow.com/questions/21136753/read-a-very-large-file-by-chunks-and-not-line-by-line

  • 3
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要在C#读取文本文件行数,可以使用以下代码: ```csharp using System; using System.IO; class Program { static void Main(string\[\] args) { string filePath = "L:\\Code\\1.txt"; // 文件路径 int lineCount = GetLineCount(filePath); // 调用方法获取行数 Console.WriteLine("该文件一共有" + lineCount + "行"); Console.ReadKey(); } public static int GetLineCount(string filePath) { int lineCount = 0; using (StreamReader sr = new StreamReader(filePath)) { while (sr.ReadLine() != null) { lineCount++; } } return lineCount; } } ``` 以上代码中,`GetLineCount`方法接收一个文件路径作为参数,使用`StreamReader`读取文件的每一行,并通过计数器统计行数。最后返回行数。在`Main`方法中,我们调用`GetLineCount`方法并输出行数。请注意,你需要将文件路径`filePath`替换为你实际的文件路径。\[1\] #### 引用[.reference_title] - *1* [C#读取txt文本内容行数](https://blog.csdn.net/qq_22889875/article/details/77924840)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C#语言读取txt行列数据](https://blog.csdn.net/chengoes/article/details/121409682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [通过程序 VB.Net 或 C# 读取文本文件行数](https://blog.csdn.net/weixin_34218890/article/details/86309622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路边闲人2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值