c# – Creating and writing files via streams

Question:

There is a class for generating a string of a certain format:

random date in last 5 years || random set of 10 Latin characters || random set of 10 Russian characters || random positive even integer between 1 and 100,000,000 || random positive number with 8 decimal places in the range from 1 to 20

Sample output:

03.03.2015||ZAwRbpGUiK||мДМЮаНкуКД||14152932||7,87742021||
23.01.2015||vgHKThbgrP||ЛДКХысХшЗЦ||35085588||8,49822372||
17.10.2017||AuTVNvaGRB||мЧепрИецрА||34259646||17,7248118||
24.09.2014||ArIAASwOnE||ЧпЙМдШлыфУ||23252734||14,6239438||
16.10.2017||eUkiAhUWmZ||ЗэЖЫзЯШАэШ||27831190||8,10838026||

Class:

class GenFile
{
    public GenFile()
    {
    }

    private Random gen = new Random();

    private DateTime RandomDay()
    {
        DateTime start = new DateTime(2013, 1, 1);
        int range = (DateTime.Today - start).Days;
        return start.AddDays(gen.Next(range));
    }

    private string RandomString(string lang)
    {
        string data = null;
        const string engChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        const string rusChars = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя";

        if (lang == "eng")
        {
            data = new string(Enumerable.Repeat(engChars, 10)
              .Select(s => s[gen.Next(s.Length)]).ToArray());
        }
        else if (lang == "rus")
        {
            data = new string(Enumerable.Repeat(rusChars, 10)
              .Select(s => s[gen.Next(s.Length)]).ToArray());
        }
        return data;
    }

    private int RandomIntNumber()
    {
        int number;
        int min = 1;
        int max = 100000000;

        return number = (2 * gen.Next(min / 2, max / 2));
    }

    private double RandomDoubleNumber()
    {
        int min = 1;
        int max = 20;
        var next = gen.NextDouble();

        return min + (next * (max - min));
    }

    public override string ToString()
    {
        return (RandomDay().ToString("d") + "||" + RandomString("eng") + "||"
            + RandomString("rus") + "||" + RandomIntNumber() + "||"
            + RandomDoubleNumber().ToString("F8") + "||");
    }        
}

When creating 100 files through a loop and writing 100,000 files to them, this process is delayed.

How I do it (very slowly):

GenFile genFile = new GenFile();
                    int i = 0;
                    while (i < 100)
                    {
                        int j = 0;
                        while (j < 100000)
                        {
                            File.AppendAllText(i + ".txt", genFile.ToString());
                            File.AppendAllText(i + ".txt", "\r\n");
                            j++;
                        }
                        i++;
                    }

Please tell me how to speed it up. Never worked with streams. I do not understand yet how to implement through flows. And is it possible at all.

Thanks in advance.

Answer:

  StringBuilder sb = new StringBuilder();
  int i = 0;
  while (i < 100)
  {
    sb.Clear();
    int j = 0;
    while (j < 100000)
    {
      sb.Append(genFile.ToString());
      sb.Append("\r\n");
      j++;
    }
    File.AppendAllText(i + ".txt", sb.ToString());
    i++;  // потерял 
  }
Scroll to Top