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

  • 相关阅读:
    HTTP/1.1 Status Code Definitions
    宽带到底“宽不宽”
    tmux的简介及使用
    Simple Gesture – Fling
    使用postfix搭建匿名smtp服务器
    dos2unix和unix2dos命令使用 [〓 脚本功略 〓]
    Android Coding: Gestures Builder: create your gestures library
    Android Gesture 手势识别使用实例 Android mobile ITeye论坛
    使用socat进行端口转发
    notepad++在编辑python文件时以4个空格代替TAB
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1807693.html
Copyright © 2011-2022 走看看