none
Stream Writer inside a loop RRS feed

  • Question

  • I'm trying to write multiple files insie a loop where I read one file and split it depending on the number of rows. The issue is in the first loop where the file has not been written.

                int i = 0;
                int totalLoopLines = 1;
                int blocks = 1;
                var filePath = @"c:\test\cars.csv"
                int rowsToSplit = 1000000;
                int totalLines = File.ReadLines(filePath).Count();
                var header = File.ReadLines(filePath).First();
    
                StreamWriter sw = null;
    
                var firstLine = true;
                
                foreach (var line in File.ReadLines(filePath).Skip(1))
                {
                    if (i >= rowsToSplit && totalLoopLines <= totalLines)
                    {
                        i = 0;
                        sw.Close();
                        sw.Dispose();
                        blocks += 1;
                        sw = new StreamWriter(directoryName + "\\" + fileName + "_" + (blocks * rowsToSplit) + ".csv");
                        sw.WriteLine(header);
                        sw.WriteLine(line);
                    }
                    else
                    {
                        if (firstLine)
                        {
                            sw = new StreamWriter(directoryName + "\\" + fileName + "_" + (blocks * rowsToSplit) + ".csv");
                            firstLine = false;
                            sw.WriteLine(header);
                        }
                        
                        sw.WriteLine(line);
                        i++;
                        
                    }
    
                    totalLoopLines += 1;
                    
                }

    Sunday, January 12, 2020 1:37 PM

Answers

  • Hi,

    You need to call Flush after WriteLine calls for writing it to the file like:

    sw.WriteLine(line);

    sw.Flush();

    or the other way is to set AutoFlush property for StreamWriter  to True :

    sw.AutoFlush = true;

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, January 12, 2020 5:55 PM

All replies

  • Hi,

    You need to call Flush after WriteLine calls for writing it to the file like:

    sw.WriteLine(line);

    sw.Flush();

    or the other way is to set AutoFlush property for StreamWriter  to True :

    sw.AutoFlush = true;

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, January 12, 2020 5:55 PM
  • Try adding sw?.Dispose() after the loop.

    And maybe reduce the number of File.ReadLines.

    • Marked as answer by FcabralJ Monday, January 13, 2020 9:39 PM
    • Unmarked as answer by FcabralJ Monday, January 13, 2020 9:42 PM
    Sunday, January 12, 2020 7:42 PM