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

删除log文件末尾中指定的行数

时间:2018-07-02 14:53:05      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:coding   i++   add   length   ram   ||   log文件   static   stream   

        /// <summary>
        /// 删除log文件末尾中指定的行数
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="line">删除的行数</param>
        public static void deleteLogToLine(string file,int line)
        {
            System.IO.StreamReader st= new StreamReader(file, System.Text.Encoding.Default);

            int lineCount = 0;//总行数
            List<string> data = new List<string>();
            while (true)
            {
                string str = st.ReadLine();
                if (str == null || str.Length == 0) break;
                data.Add(str);
                lineCount++;
            }
            st.Close();
            st.Dispose();

            StreamWriter sw = new StreamWriter(file, false);
            for (int i = 0; i < data.Count - line; i++)
            {
                sw.WriteLineAsync(data[i]);
            }
            sw.Flush();
            sw.Close();
        }

删除log文件末尾中指定的行数

标签:coding   i++   add   length   ram   ||   log文件   static   stream   

原文地址:https://www.cnblogs.com/cyqdeshenluo/p/9253614.html

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