创建单独的文件 [英] Create seperate files

查看:56
本文介绍了创建单独的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有这个:



Hi everybody,

I have this:

static void Main(string[] args)
    {


        const int linesPerFile = 10;
        const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";
        //string fileName = "File";






            var fileCounter = 0;
            var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
            try
            {

                // foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                //{
                var lineCounter = 0;
                string line;

                while ((line = destiNationFile.NewLine) != null)
                {
                    foreach (var bank in BankAcoutNumbers.BANS.Take(200))
                    {
                        if (lineCounter >= linesPerFile)
                        {
                            lineCounter = 0;
                            fileCounter++;
                            destiNationFile.Dispose();
                            destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));

                        }

                        destiNationFile.WriteLine(bank);
                        lineCounter++;
                    }
                    //}


                }
            }
            catch (Exception)
            {

                throw;
            }



        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }





但问题是它会创建无穷无尽的文件。不是20个文件 - 它必须是什么。



谢谢



我现在有这样的:







But the problem is it creates endless files. Not 20 files - what it has to be.

Thank you

I have it now like this:


static void Main(string[] args)
    {


        const int linesPerFile = 10;
        const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";
        //string fileName = "File";
        var maxNumberOfFiles = 10;
        Stopwatch timer = new Stopwatch();



            var fileCounter = 0;

            var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
            try
            {

                // foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                //{
                var lineCounter = 0;
                string line;

                while (fileCounter <= maxNumberOfFiles)
                {
                    timer.Start();
                    foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                    {
                        if (lineCounter % linesPerFile == 0)
                        {
                            //lineCounter = 0;
                            destiNationFile.Flush();
                            destiNationFile.Dispose();
                            destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
                            fileCounter++;
                        }

                        destiNationFile.WriteLine(bank);
                        lineCounter++;

                    }

                    fileCounter++;

                    //}


                }
                timer.Stop();
                Console.WriteLine(timer.Elapsed.Seconds);
            }
            catch (Exception)
            {

                throw;
            }



        // Keep the console window open in debug mode.

        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }



但我得到10个fiels,所以这是正确的10 * 10。但问题是每次第一个文件为空时


But I get ten fiels, so that is correct 10*10. But the problem is that every time the tinth file is empty

推荐答案

问题在于你的while循环条件,它永远不会变为假,所以永远不会停止



你应该把最终评估的东西放到假的地方,比如



The problem is with your while loop condition, it never gets to false so never stops

You should put something that eventually evaluates to false, something like

var maxNumberOfFiles= 20

while (fileCounter < maxNumberOfFiles) {

//your code here

fileCounter++;

}


这是因为你有一个赋值操作作为你的哨兵,并且赋值总是返回true。在sentinals中的任务是一个巨大的反模式



我怀疑你正在尝试这样做:



That's becuase you have an assignment operation as your sentinel, and assignments always return true. Assignments in sentinals are a giant anti-pattern

I suspect you're trying to do this:

var fileCounter = 1;
var lineCounter = 0
var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter));
foreach(var bank in BankAcoutNumbers.BANS.Take(200))
{
    if(lineCounter % linesPerFile == 0)
    {
        destiNationFile.Flush();
        destiNationFile.Dispose();
        destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter));
        fileCounter++;
    } 
    destiNationFile.WriteLine(bank);
    lineCounter++;
} 





哦,然后你把它丢弃在循环中,这是糟糕的juju,因为你的休息条件现在没有内存中存在更长的时间使用递归函数,而不是while循环。



while循环看起来毫无意义,所以这里是一个更简洁的副本。



Oh, and then you dispose of it in the loop, which is bad juju since your break condition now no longer exists in memory. Use a recursive function for this, not a while loop.

The while loop looks to be meaningless, so here's a more concise copy.


这篇关于创建单独的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆