直接写入文件系统

时间:2015-01-22 10:07:16

标签: c#-5.0

我想直接写入文件系统,而不是每一行写入字符串,因为这更耗时。

我这样试试:

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

我这样试试:

  

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

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

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

                        }

谢谢

我这样试试:

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

                            }

但我没看到文件的内容。文件为空

我这样试试:

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 个答案:

答案 0 :(得分:0)

WriteAllText并不比StreamWriter慢。 从.Net源代码中可以看出WriteAllText的工作原理:

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

缓慢是由字符串引起的。如果用StringBuilder替换字符串性能会提高。检查此代码:

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