将文本文件拆分为较小的文件

时间:2012-07-19 12:26:51

标签: c#

我有一个1000 000记录文本文件,所以我想将文件拆分成多个文件,每个文件有100条记录。这是我使用listbox1来控制文件的代码。代码正在运行,但丢失的记录较少。

private void WriteToFile()
    {
        int RowCount = listBox1.Items.Count;
        string FileName = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA";
        StreamWriter sw = new StreamWriter(FileName + ".txt");
        int inc = 0;
        int counter = 0;
        //StreamWriter sw = new StreamWriter(FileName+inc + ".txt");
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
             sw.WriteLine(listBox1.Items[i].ToString());
             string me = listBox1.Items[i].ToString();

             if (RowCount > 100)
             {

                 listBox2.Items.Add(listBox1.Items[counter].ToString());
                 counter++;
                 if (counter == 100)
                 {

                     inc++;
                     sw = new StreamWriter(FileName + inc + ".txt");


                     RowCount = RowCount - counter;
                     counter = 0;

                 }
             }

             else
             {
                  sw.WriteLine(listBox1.Items[i].ToString());
             }

         }
         sw.Close();
    }

3 个答案:

答案 0 :(得分:0)

在这一行:

sw = new StreamWriter(FileName + inc + ".txt");

你需要.Flush()前一个sw。编写器是缓冲的,这就是为什么缺少某些记录的原因。

答案 1 :(得分:0)

我不确定您的问题与您的ListBox有何关联,因此我将向您展示一个解决方案,该解决方案每100行从一个巨大的文件创建文件。

LinqEnumerable.GroupBy

很容易
int maxLineCount = 100;
FileInfo file = new FileInfo(hugeFilePath);

var fileGroups = File.ReadLines(file.FullName)
    .Select((l, i) => new { Line = l, Index = i })
    .GroupBy(x => x.Index / maxLineCount)
    .Select(grp => new { FileIndex = grp.Key, Lines = grp.Select(x => x.Line)});

foreach (var grp in fileGroups)
{
    var fileName = "File" + grp.FileIndex;
    var path = Path.Combine(@"C:\Temp\Test", fileName + file.Extension).ToString();
    File.WriteAllLines(path, grp.Lines);
}

请注意,File.ReadLines会对行进行流式处理,而不是将所有行加载到内存中。

答案 2 :(得分:0)

这是一种更简单的方法:

private void WriteToFile() 
{ 
    // get an array of strings - you'll find out way :)
    string[] items = listBox1.Items.Cast<string>().ToArray();

    // this would also work with ReadAllLines()
    string[] items = File.ReadAllLines("Your original file");

    // path + filename prefix
    string fileNamePattern = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA{0}.txt"; 

    // blocks of 100
    string[] buffer;
    for(int i = 0; i < items.Length; i += 100)
    {
        // slice the string array into 100 string blocks
        buffer = items.Slice(i, 100);

        // output the block of strings
        File.WriteAllLines(string.Format(fileNamePattern, i), buffer);
    }
}

切片扩展:

    public static T[] Slice<T>(this T[] source, int index, int length)
    {
        T[] slice = new T[length];
        Array.Copy(source, index, slice, 0, length);
        return slice;
    }