码迷,mamicode.com
首页 > 其他好文 > 详细

删除大日志文件中的某段数据

时间:2015-04-29 19:40:03      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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);
        }
    }
}

 

删除大日志文件中的某段数据

标签:

原文地址:http://www.cnblogs.com/zhuhc/p/4466762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!