删除大日志文件中的某段数据
using System; using System.IO; using System.Linq; using System.Text; namespace TestMultyConsole2 { public class LocalFileHelper { /// <summary> /// 删除大日志文件中的某些数据 /// </summary> /// <param name="filePath">源文件路径</param> /// <param name="newFilePath">目标文件路径</param> /// <param name="key">删除key之间的数据(包含key)</param> /// <param name="words">删除某一含有该列表中数据的数据行</param> public static void CopyContentAndDeleteSomething(string filePath, string newFilePath,string key, string[] words) { bool flag = false; //标识符 long count = 0; //计数器 using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312"))) using (StreamWriter streamWriter = new StreamWriter(newFilePath, true, Encoding.GetEncoding("gb2312"))) { while (sr.Peek() >= 0) { string tmp = sr.ReadLine(); if (tmp != null && tmp.Contains(key)) { flag = !flag; } if (!flag && !IsContains(tmp, words)) streamWriter.WriteLine(tmp); if (count++%10000==0) Console.WriteLine(count/10000); } } Console.WriteLine("共{0}行数据", count); } /// <summary> /// 判断keys中的值是否被包含在content中 /// </summary> /// <param name="content">数据源</param> /// <param name="keys">key列表</param> /// <returns></returns> public static bool IsContains(string content, string[] keys) { return keys != null && keys.Any(content.Contains); } } }