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")这个主要是防止读取中文时乱码用的。

  • 相关阅读:
    TC SRM 591 (Div2. Practice only)
    SDL2 简单实现图片缩放移动查看
    Linux下socket编程 address already in use 问题
    POJ 2155 二维树状数组
    OJ开发笔记(1)
    开通博客啦~
    [转]STL transform算法中使用toupper函数
    Monkey and Banana HDU 1069
    Ignatius and the Princess IV HDU 1029
    Dungeon Master POJ 2251
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1807693.html
Copyright © 2011-2022 走看看