zoukankan      html  css  js  c++  java
  • 利用LINQ读行文本&去重复

     1         static private void GetOneText(string filePath)
     2         {
     3             using (StreamReader sr = new StreamReader(filePath,Encoding.GetEncoding("gb2312")))
     4             {
     5                 List<string> listQuery = new List<string>();
     6                 for(string line=sr.ReadLine();line!=null;line=sr.ReadLine())
     7                 {
     8                     if (!string.IsNullOrEmpty(line))
     9                         listQuery.Add(line);
    10                 }
    11                 var query = listQuery.Distinct();
    12                 string newfilePath = @"d:\2.txt";
    13                 using (FileStream fs = new FileStream(newfilePath, FileMode.Create))
    14                 using (TextWriter tw = new StreamWriter(fs))
    15                 {
    16                     foreach(var ary in query)
    17                     {
    18                         tw.WriteLine(ary.ToString());
    19                     }
    20                 }
    21             }
    22         }
    23         static void Main(string[] args)
    24         {
    25             string filePath = @"d:\1.txt";
    26             GetOneText(filePath);
    27         }

    以前也遇到类似问题,是用System.IO+正则完成的~这个是Linq版本的 记录一下

    Encoding.GetEncoding("gb2312")这个主要是防止读取中文时乱码用的。

  • 相关阅读:
    修改其他输入法为android 默认输入法
    {php 数据类型}
    转:PHP.ini配置文件(中文)
    php导出任意mysql数据库中的表去excel文件
    php输出、写入csv
    smarty二级分类代码和模版循环例子
    笔记:使ecshop 模板中可引用 常量
    Windows下Apache2不同域名解析不同目录解决方法
    {php 数组}
    Apache中.htaccess文件功能
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1807693.html
Copyright © 2011-2022 走看看