在C#中的行数拆分大文件分成更小的文件? [英] Split large file into smaller files by number of lines in C#?

查看:431
本文介绍了在C#中的行数拆分大文件分成更小的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何在每个文件的行数拆分文件。该文件是CSV和我不能用字节做。我需要用线来做到这一点。 20K似乎是每个文件好一些。这是在给​​定位置读取一个流的最佳方法? Stream.BaseStream.Position?所以,如果我读的第一20K行,我会开始在39999的位置?我怎么知道我几乎在一个文件结束了吗?感谢所有

解决方案

 使用(就是System.IO.StreamReader SR =新就是System.IO.StreamReader(路径))
{
    INT fileNumber = 0;

    而(!sr.EndOfStream)
    {
        诠释计数= 0;

        使用(System.IO.StreamWriter SW =新System.IO.StreamWriter(另一条道路+ + + fileNumber))
        {
            sw.AutoFlush = TRUE;

            而(sr.EndOfStream&安培;!&安培; ++计数< 20000)
            {
                sw.WriteLine(sr.ReadLine());
            }
        }
    }
}
 

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 good number per file. What is the best way to read a stream at a given position? Stream.BaseStream.Position? So if I read the first 20k lines i would start the position at 39,999? How do I know I am almost at the end of a files? Thanks all

解决方案

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

这篇关于在C#中的行数拆分大文件分成更小的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆