zoukankan      html  css  js  c++  java
  • C# 大文本拆分

    
    public static void SplitBigTxtFile(string filePath)
            {
                int lineOfEach = 500000;
                int fileIndex = 1;
    
                var outputFile = Path.GetFileNameWithoutExtension(filePath) + "_" + fileIndex + ".txt";
                string headerLine = "";
                int lineIndex = 1;
    
                StreamWriter sw = new StreamWriter(outputFile, false, Encoding.GetEncoding("gb2312"));
                using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312")))
                {
                    headerLine = sr.ReadLine();
                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        lineIndex++;
                        sw.WriteLine(line);
    
                        if (lineIndex % lineOfEach == 0)
                        {
                            fileIndex++;
                            outputFile = Path.GetFileNameWithoutExtension(filePath) + "_" + fileIndex + ".txt";
                            sw.Flush();
                            sw.Close();
    
                            sw = new StreamWriter(outputFile, false, Encoding.GetEncoding("gb2312"));
                        }
                    }
                }
            }
    
  • 相关阅读:
    C#中IDisposable学习
    C# volatile与lock
    TFS源代码管理
    C#如何获取真实IP地址
    c# 协变和逆变
    基本数据类型
    用户交互
    变量.常量
    输出语句,注释,
    基础知识随笔
  • 原文地址:https://www.cnblogs.com/LukeSteven/p/15476319.html
Copyright © 2011-2022 走看看