Writinging directly to filesystem

点点圈 提交于 2019-12-14 04:14:30

问题


I want to directly writing to file system and not every line to a string, because that is much more time consuming.

I try it like this:

static void Main(string[] args)
        {

            string path = @"C:\BankNumber";
            var bans = BankAcoutNumbers.BANS;
            const int MAX_FILES = 80;
            const int BANS_PER_FILE = 8181 / 80;
            int bansCounter = 0;
            var part = new List<int>();
            var maxNumberOfFiles = 10;
            Stopwatch timer = new Stopwatch();
            var fileCounter = 0;



            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }

            try
            {
                while (fileCounter <= maxNumberOfFiles)
                {
                    timer.Start();
                    Console.WriteLine("Start writing the bank numbers to file system");
                   // Console.WriteLine(timer.Start());
                    foreach (var bank in BankAcoutNumbers.BANS)
                    {
                        part.Add(bank);
                        if (++bansCounter >= BANS_PER_FILE)
                        {
                            string fileName = string.Format("{0}.txt-{1}.txt", part[0], part[part.Count - 1]);
                            string outputToFile = "";// Otherwise you dont see the lines in the file. Just single line!!


                            string subString = System.IO.Path.Combine(path, "nr");//Needed to add, because otherwise the files will not stored in the correct folder!!
                            fileName =  subString + fileName;

                            foreach (var partBan in part)
                            {
                                using(StreamWriter st = new StreamWriter(fileName))
                                {

                                    System.IO.File.WriteAllText(fileName, outputToFile);
                                }

                                //Console.WriteLine(partBan);
                               // outputToFile += partBan + Environment.NewLine;//Writing the lines to the file

                            }
                            ;//Writes to file system.
                            part.Clear();
                            bansCounter = 0;
                            //System.IO.File.WriteAllText(fileName, part.ToString());

                            if (++fileCounter >= MAX_FILES)
                                break;
                        }
                    }
                }

                timer.Stop();
                Console.WriteLine("Total time of writing the bank numbers to file system " + timer.Elapsed.Seconds + " seconds");
                //Console.WriteLine(BankAcoutNumbers.BANS.Count());
            }
            catch (Exception)
            {

                throw;
            }

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

I try it like this:

foreach (var partBan in part) { using(StreamWriter st = new StreamWriter(fileName)) {

                                System.IO.File.WriteAllText(fileName, outputToFile);
                            }

                            //Console.WriteLine(partBan);
                           // outputToFile += partBan + Environment.NewLine;//Writing the lines to the file

                        }

Thank you

I try it like this:

foreach (var partBan in part)
                            {
                                using(StreamWriter st = new StreamWriter(fileName))
                                {

                                    //System.IO.File.WriteAllText(subString, fileName);
                                    st.WriteLine(outputToFile);
                                }

                                //Console.WriteLine(partBan);
                               // outputToFile += partBan + Environment.NewLine;//Writing the lines to the file

                            }

But I dont see the content of the files. The files are empty

I try it like this:

public class Program
    {

        static void Main(string[] args)
        {

            string path = @"C:\BankNumber";
            var bans = BankAcoutNumbers.BANS;
            const int MAX_FILES = 80;
            const int BANS_PER_FILE = 81818182 / 80;
            int bansCounter = 0;
            var part = new List<int>();
            var maxNumberOfFiles = 10;
            Stopwatch timer = new Stopwatch();
            var fileCounter = 0;



            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }

            try
            {
                while (fileCounter <= maxNumberOfFiles)
                {
                    timer.Start();
                    Console.WriteLine("Start writing the bank numbers to file system");
                   // Console.WriteLine(timer.Start());
                    foreach (var bank in BankAcoutNumbers.BANS)
                    {
                        part.Add(bank);
                        if (++bansCounter >= BANS_PER_FILE)
                        {
                            string fileName = string.Format("{0}.txt-{1}.txt", part[0], part[part.Count - 1]);
                            //string outputToFile = "";// Otherwise you dont see the lines in the file. Just single line!!
                            StringBuilder OutputFile = new StringBuilder("");


                            string subString = System.IO.Path.Combine(path, "nr");//Needed to add, because otherwise the files will not stored in the correct folder!!
                            fileName =  subString + fileName;

                            foreach (var partBan in part)
                            {
                                OutputFile.Append(string.Format("{0}{1}", partBan.ToString(), Environment.NewLine));


                                //using(StreamWriter st = new StreamWriter(fileName))
                                //{

                                //    //System.IO.File.WriteAllText(subString, fileName);
                                //    StringBuilder strBuilder = new StringBuilder();
                                //    strBuilder.Append(st.)
                                //    st.Write(partBan + Environment.NewLine);
                                //}

                                //Console.WriteLine(partBan);
                               // outputToFile += partBan + Environment.NewLine;//Writing the lines to the file

                            }
                            //;//Writes to file system.
                            System.IO.File.WriteAllText(fileName, OutputFile.ToString());
                            part.Clear();
                            bansCounter = 0;
                            //System.IO.File.WriteAllText(fileName, part.ToString());

                            if (++fileCounter >= MAX_FILES)
                                break;
                        }
                    }
                }

                timer.Stop();
                Console.WriteLine("Total time of writing the bank numbers to file system " + timer.Elapsed.TotalSeconds + " seconds");
                //Console.WriteLine(BankAcoutNumbers.BANS.Count());
            }
            catch (Exception)
            {

                throw;
            }

            foreach (var item in part)
            {
                ThreadPool.QueueUserWorkItem(DoLongTask);
            }


            Console.WriteLine("Main thread ends");


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

        public static void DoLongTask(object input)
        {
            Console.WriteLine("Thread is background : {0}", Thread.CurrentThread.IsBackground);
            Console.WriteLine("Input parameter : {0}", input);
        }

    }

回答1:


WriteAllText is not slower then StreamWriter. From the .Net source code here it how WriteAllText works:

private static void InternalWriteAllText(string path,
    string contents, Encoding encoding)
{
    Contract.Requires(path != null);
    Contract.Requires(encoding != null);
    Contract.Requires(path.Length > 0);
    using (StreamWriter sw = new StreamWriter(path, false, encoding))
        sw.Write(contents);
}

Slowness is caused by string. If to substitute string with StringBuilder perfomance will improve. Check this code:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;

    namespace ConsoleApplication1
    {
        class BankAcoutNumbers
        {
            public List<int> BANS { get; set; }

            public BankAcoutNumbers()
            {
                BANS = new List<int>();
                BANS.Add(1456456465);
                BANS.Add(2456465);
                BANS.Add(342346465);
                BANS.Add(445645646);
                BANS.Add(545636546);
                BANS.Add(64556465);
                BANS.Add(7456465);
                BANS.Add(842346465);
                BANS.Add(9456456);
                BANS.Add(10456365);
                BANS.Add(11456456);
                BANS.Add(12456465);
                BANS.Add(1342346);
                BANS.Add(1445645);
                BANS.Add(1545636);
                BANS.Add(1645645);
                BANS.Add(1745646);
                BANS.Add(1842345);
                BANS.Add(194564);
                BANS.Add(2045635);
                BANS.Add(214564);
                BANS.Add(224564);
                BANS.Add(234234);
                BANS.Add(244564);
                BANS.Add(254563);
            }
        }


        class Program
        {
            static void Main(string[] args)
            {

                string path = @"C:\";

                //string fileName = string.Format("{0}{1}-", part[0], part[part.Count - 1]);
                //var bans = BankAcoutNumbers.BANS;
                const int MAX_FILES = 10;
                const int BANS_PER_FILE = 10;
                int bansCounter = 0;
                var part = new List<int>();
                //string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]);

                var maxNumberOfFiles = 10;
                Stopwatch timer = new Stopwatch();



                var fileCounter = 0;

                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                }

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

                    // foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                    //{


                    while (fileCounter <= maxNumberOfFiles)
                    {
                        timer.Start();
                        foreach (var bank in new BankAcoutNumbers().BANS)
                        {
                            part.Add(bank);
                            if (++bansCounter >= BANS_PER_FILE)
                            {
                                string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]);
                                StringBuilder  outputToFile = new StringBuilder("");
                                //var destinationFile = new StreamWriter(fileName);
                                //destiNationFile = new StreamWriter(fileName);
                                Console.WriteLine("NR{0}", fileName);
                                fileName = @"C:\" + fileName;
                                foreach (var partBan in part)
                                {
                                    outputToFile.Append(string.Format("{0}{1}", partBan , Environment.NewLine));
                                    Console.WriteLine(partBan);
                                }
                                System.IO.File.WriteAllText(fileName, outputToFile.ToString());
                                part.Clear();
                                bansCounter = 0;

                                if (++fileCounter >= MAX_FILES)
                                    break;


                            }


                        }

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


来源:https://stackoverflow.com/questions/28085999/writinging-directly-to-filesystem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!