快速更换大文件的第一行 [英] Quickly replace first line of large file

查看:138
本文介绍了快速更换大文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我导入到数据库中许多大的CSV文件(每个1-10 GB)。对于每一个文件,我需要更换1号线,所以我可以格式化头是列名。我目前的解决办法是:

I have many large csv files (1-10 gb each) which I'm importing into databases. For each file, I need to replace the 1st line so I can format the headers to be the column names. My current solution is:

using (var reader = new StreamReader(file))
{
    using (var writer = new StreamWriter(fixed))
    {
        var line = reader.ReadLine();
        var fixedLine = parseHeaders(line);
        writer.WriteLine(fixedLine);

        while ((line = reader.ReadLine()) != null)
            writer.WriteLine(line);
    }
}



什么是更快的方式只能更换1号线无通过这些庞大的文件,每隔一行迭代?

What is a quicker way to only replace line 1 without iterating through every other line of these huge files?

推荐答案

如果你能保证固网的长度相同(或更少)为 行,您可以更新,而不是复制它们就地文件。

If you can guarantee that fixedLine is the same length (or less) as line, you can update the files in-place instead of copying them.

如果不是,你都不可能通过访问你的的StreamReader .BaseStream 得到一点点的性能提升>和的StreamWriter 和做块大副本(使用,比如说,一个32K字节的缓冲区)做拷贝,这至少会消除所花费的时间检查每个字符,看是否它的尾行的字符作为现在 reader.ReadLine()

If not, you can possibly get a little performance improvement by accessing the .BaseStream of your StreamReader and StreamWriter and doing big block copies (using, say, a 32K byte buffer) to do the copying, which will at least eliminate the time spent checking every character to see if it's an end-of-line character as happens now with reader.ReadLine().

这篇关于快速更换大文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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