用for循环中的参数写入文件名

时间:2015-05-01 05:52:19

标签: c# streamwriter streamwriter.write

我想创建一个包含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”打开了。我得到了 “numbers0.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++;
        }
    }

并且numbers0.txt之后的所有文件都是空的。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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();