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

  • 相关阅读:
    【Docker】解析器指令之 escape
    【Docker】解析器指令之 syntax
    【Docker】Dockerfile 解析器指令
    【Docker】Dockerfile 格式
    【Docker】Dockerfile 介绍
    【Docker】Spring Boot 和 Docker
    【Docker】理解 Docker 中的 OverlayFS
    【Docker】使用 OverlayFS 存储驱动
    【Docker】选择存储驱动
    kuangbin专题 专题一 简单搜索 Fire! UVA
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1807693.html
Copyright © 2011-2022 走看看