Split large file into smaller files by number of lines in C#?

前端 未结 3 1256
一向
一向 2021-02-06 18:58

I am trying to figure out how to split a file by the number of lines in each file. THe files are csv and I can\'t do it by bytes. I need to do it by lines. 20k seems to be a goo

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 19:29

    using (System.IO.StreamReader sr = new System.IO.StreamReader("path"))
    {
        int fileNumber = 0;
    
        while (!sr.EndOfStream)
        {
            int count = 0;
    
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("other path" + ++fileNumber))
            {
                sw.AutoFlush = true;
    
                while (!sr.EndOfStream && ++count < 20000)
                {
                    sw.WriteLine(sr.ReadLine());
                }
            }
        }
    }
    

提交回复
热议问题