如何编辑文件,在开头附加数据,而不重写整个文件?

时间:2017-04-07 13:13:50

标签: c#

我有一个csv文件,我需要根据文件的前两个字符添加一个唯一的ID。我有以下代码:

using (StreamReader sr = new StreamReader(f))
                    {
                        string currentLine;
                        int id = 0;

                        while ((currentLine = sr.ReadLine()) != null)
                        {
                            string row = currentLine.ToString();
                            string FirstTwoCharacters = currentLine.Substring(0, 2);


                            if (FirstTwoCharacters == "01")
                            {
                                id = id + 1;
                                row += "*" + id.ToString();

                                using (StreamWriter files = File.AppendText(dir + newfilename))
                                {
                                    files.WriteLine(row);
                                }
                            }
                            else
                            {
                                row += "*" + id.ToString();

                                using (StreamWriter files = File.AppendText(dir + newfilename))
                                {
                                    files.WriteLine(row);
                                }
                            }

                        }
                    }

csv文件可能很大,1Gb大小,大约600万行。只是需要建议,如果有更快的方法来处理这个,因为它目前可能需要3个多小时来处理文件,并且可以一次接收多个文件。

2 个答案:

答案 0 :(得分:1)

不是为每行输入文件打开新文件以附加行,而是可以保持流编写器打开:

using (StreamReader sr = new StreamReader(f))
using (StreamWriter files = File.AppendText(dir + newfilename))
{
    string currentLine;
    int id = 0;

    while ((currentLine = sr.ReadLine()) != null)
    {
        string firstTwoCharacters = currentLine.Substring(0, 2);

        if (firstTwoCharacters == "01")
             id++;

        files.WriteLine(currentLine + "*" + id);
    }
}

您还可以使用File.ReadLines枚举源代码行:

using (StreamWriter writer = File.AppendText(dir + newfilename))
{
    int id = 0;

    foreach(var line in File.ReadLines(f))
    {
        if (line.Substring(0,2) == "01")
           id++;

        writer.WriteLine($"{line}*{id}");
    }
}

甚至是LINQ方法

int id = 0;
var newLines = from line in File.ReadLines(f)
               let incrementId = line.Substring(0,2) == "01"
               select $"{line}*{incrementId ? (++id) : id}";

File.WriteAllLines(dir + newfilename, newLines);

答案 1 :(得分:0)

File.AppendText循环中打开(while)代价很高,请将其移至while

之外
using (StreamReader sr = new StreamReader(f))
{
                string currentLine;
                int id = 0;
                using (StreamWriter files = File.AppendText(dir + newfilename))
                {
                    while ((currentLine = sr.ReadLine()) != null)
                    {
                        string row = currentLine.ToString();
                        string FirstTwoCharacters = currentLine.Substring(0, 2);


                        if (FirstTwoCharacters == "01")
                        {
                            id = id + 1;
                            row += "*" + id.ToString();

                            files.WriteLine(row);

                        }
                        else
                        {
                            row += "*" + id.ToString();

                            files.WriteLine(row);

                        }

                    }
                }
            }