0

我有一个 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();
    }
4

3 回答 3

0

在这一行:

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

您需要 .Flush() 前一个 sw。写入器已缓冲,这就是缺少某些记录的原因。

于 2012-07-19T12:45:41.457 回答
0

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

Linq使用and很容易Enumerable.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流式传输行而不是将所有行加载到内存中。

于 2012-07-19T12:46:22.060 回答
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;
    }
于 2012-07-19T12:55:50.737 回答