写入文件名,其中包含循环中的参数



我想创建一个列表,其中包含 0000000 - 9999999 之间的所有数字,并每 50000 行创建一个新文件。

我尝试了以下代码:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:numbers" + fileparam + ".txt"))
{
    for (counter = 0 ; counter < 10000000 ; counter++)
    {
        if (counter < 10)
        {
            file.WriteLine("000000" + counter.ToString());
        }
        else if (counter > 9 && counter < 100)
        {
            file.WriteLine("00000" + counter.ToString());
        }
        else if (counter > 99 && counter < 1000)
        {
            file.WriteLine("0000" + counter.ToString());
        }
        else if (counter > 999 && counter < 10000)
        {
            file.WriteLine("000" + counter.ToString());
        }
        else if (counter > 9999 && counter < 100000)
        {
            file.WriteLine("00" + counter.ToString());
        }
        else if (counter > 99999 && counter < 1000000)
        {
            file.WriteLine("0" + counter.ToString());
        }
        else if (counter > 999999 && counter < 10000000)
        {
            file.WriteLine(counter.ToString());
        }
        if (counter % 50000 == 0)
        {
            fileparam++;
        }
    }
}

但它不起作用,因为它已经打开了在程序"int fileparam = 0;"开始时初始化的"Fileparam",我得到了"数字0.txt"

我也试过这个:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:numbers" + fileparam + ".txt"))
{
    for (counter = 0 ; counter < 10000000 ; counter++)
    {
        if (counter < 10)
        {
            file.WriteLine("000000" + counter.ToString());
        }
        else if (counter > 9 && counter < 100)
        {
            file.WriteLine("00000" + counter.ToString());
        }
        else if (counter > 99 && counter < 1000)
        {
            file.WriteLine("0000" + counter.ToString());
        }
        else if (counter > 999 && counter < 10000)
        {
            file.WriteLine("000" + counter.ToString());
        }
        else if (counter > 9999 && counter < 100000)
        {
            file.WriteLine("00" + counter.ToString());
        }
        else if (counter > 99999 && counter < 1000000)
        {
            file.WriteLine("0" + counter.ToString());
        }
        else if (counter > 999999 && counter < 10000000)
        {
            using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"E:numbers" + fileparam + ".txt"))
            {
                file.WriteLine(counter.ToString());
            }
        }
        if (counter % 50000 == 0)
        {
            fileparam++;
        }
    }

并且数字 0.txt 之后的所有文件都是空的。

你可以试试这个:

var counter = 0;
var fileParam = 0;
StreamWriter sw = new StreamWriter(string.Format("C://archive/Numbers/numbers{0}.txt", fileParam));
while (counter < 10000000)
{
    sw.WriteLine(counter.ToString("D7"));
    if ((counter + 1) % 50000 == 0)
    {
        fileParam++;
        sw.Flush();
        sw.Close();
        sw = new StreamWriter(string.Format("C://archive/Numbers/numbers{0}.txt", fileParam));
    }
    counter++;
}
sw.Flush();
sw.Close();

相关内容

  • 没有找到相关文章

最新更新