Splitting of text file not working properly c#


  • I have requirement of writing to text file.

    If the file size exceeds 700mb, create new file & write to it.

    I am currently writing data with “|” delimited from database to file & after that check the file size & splitting into multiple files, but the file splits in middle of the line.

    It should write till end of line or start that particular line in new file .

    I need to write the column names in the first line in the newly splited file.

    I am new to c#, could you please suggest me the solution with the sample code.

    Please find below code to splitting the file

         private static void ReadWriteToFile(string fileNames)


                    string sourceFileName = fileNames;

                    string destFileLocation = Path.GetDirectoryName(fileNames);

                    int index = 0;

                    long maxFileSize = 700 * 1024 * 1024;

                    byte[] buffer = new byte[65536];


                    using (Stream source = File.OpenRead(sourceFileName))


                        while (source.Position < source.Length)




                            string newFileName = Path.Combine(destFileLocation, Path.GetFileNameWithoutExtension(sourceFileName));

                            newFileName += index.ToString() + Path.GetExtension(sourceFileName);

                            using (Stream destination = File.OpenWrite(newFileName))


                                while (destination.Position < maxFileSize)


                                   int bytes = source.Read(buffer, 0, (int)Math.Min(maxFileSize, buffer.Length));

                                    destination.Write(buffer, 0, bytes);


                                    if (bytes < Math.Min(maxFileSize, buffer.Length))









    Could you please let me know if there is any alternative best way to do this

    Thanks in advance.

    • Edited by thyagi9510 Tuesday, April 25, 2017 11:40 AM
    Tuesday, April 25, 2017 11:37 AM

All replies

  • In your code I see that you get the files and write until you reach the pre-determined maximum length. However, I do not see any code to separate the files based on the "|" character. I would recommend using String.Split() to split the text based on where it has "|". You can also include conditional functionality to then trim any unnecessary whitespace or characters using regular expressions or further extend the functionality of the splitting function. 

    String.Split() -

    String.Split() returns an array of strings split on the designated argument. 

    Example: string[] splitStringArray = <StreamOfText>.Split('|');

    Then use a for loop to iterate through "splitStringArray[]", trim or otherwise modify each string, and write each to its own file.

    Hope this helps!

    Thursday, June 22, 2017 4:49 PM