Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:05:05+00:00 2026-05-30T19:05:05+00:00

Morning, I’m trying to split a large text file (15,000,000 rows) using StreamReader/StreamWriter. Is

  • 0

Morning,

I’m trying to split a large text file (15,000,000 rows) using StreamReader/StreamWriter. Is there a quicker way?

I tested it with 130,000 rows and it took 2min 40sec which implies 15,000,000 rows will take approx 5hrs which seems a bit excessive.

//Perform split.
public void SplitFiles(int[] newFiles, string filePath, int processorCount)
{
    using (StreamReader Reader = new StreamReader(filePath))
    {
        for (int i = 0; i < newFiles.Length; i++)
        {
            string extension = System.IO.Path.GetExtension(filePath);
            string temp = filePath.Substring(0, filePath.Length - extension.Length)
                              + i.ToString();
            string FilePath = temp + extension;

            if (!File.Exists(FilePath))
            {
                for (int x = 0; x < newFiles[i]; x++)
                {
                    DataWriter(Reader.ReadLine(), FilePath);
                }
            }
            else
            {
                return;
            }
        }
    }
}

public void DataWriter(string rowData, string filePath)
{
    bool appendData = true;
    using (StreamWriter sr = new StreamWriter(filePath, appendData))
    {
        {
            sr.WriteLine(rowData);
        }
    }
}

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:05:06+00:00Added an answer on May 30, 2026 at 7:05 pm

    You haven’t made it very clear, but I’m assuming that the value of each element of the newFiles array is the number of lines to copy from the original into that file. Note that currently you don’t detect the situation where there’s either extra data at the end of the input file, or it’s shorter than expected. I suspect you want something like this:

    public void SplitFiles(int[] newFiles, string inputFile)
    {
        string baseName = Path.GetFileNameWithoutExtension(inputFile);
        string extension = Path.GetExtension(inputFile);
        using (TextReader reader = File.OpenText(inputFile))
        {
            for (int i = 0; i < newFiles.Length; i++)
            {
                string outputFile = baseName + i + extension;
                if (File.Exists(outputFile))
                {
                    // Better than silently returning, I'd suggest...
                    throw new IOException("File already exists: " + outputFile);
                }
    
                int linesToCopy = newFiles[i];
                using (TextWriter writer = File.CreateText(outputFile))
                {
                    for (int j = 0; i < linesToCopy; j++)
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                        {
                            return; // Premature end of input
                        }
                        writer.WriteLine(line);
                    }
                }
            }
        }
    }
    

    Note that this still won’t detect if there’s any unconsumed input… it’s not clear what you want to do in that situation.

    One option for code clarity is to extract the middle of this into a separate method:

    public void SplitFiles(int[] newFiles, string inputFile)
    {
        string baseName = Path.GetFileNameWithoutExtension(inputFile);
        string extension = Path.GetExtension(inputFile);
        using (TextReader reader = File.OpenText(inputFile))
        {
            for (int i = 0; i < newFiles.Length; i++)
            {
                string outputFile = baseName + i + extension;
                // Could put this into the CopyLines method if you wanted
                if (File.Exists(outputFile))
                {
                    // Better than silently returning, I'd suggest...
                    throw new IOException("File already exists: " + outputFile);
                }
    
                CopyLines(reader, outputFile, newFiles[i]);
            }
        }
    }
    
    private static void CopyLines(TextReader reader, string outputFile, int count)
    {
        using (TextWriter writer = File.CreateText(outputFile))
        {
            for (int i = 0; i < count; i++)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    return; // Premature end of input
                }
                writer.WriteLine(line);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Morning folks, I have a temporary table with 135,000 rows and 24 columns, of
Good morning stackoverflow ! I'm trying to extract from a binary file a hexadecimal
This morning I ran into an issue with returning back a text string as
Good morning. I have an XML file which contains lists of warning and errors
morning all, I've been browsing around trying to work out what I've done wrong
Morning y'all! Basically, I'm using a table to store my main data - called
Morning all Now I know there is a reason to this odering but my
Morning folks, I'm still trying to get my head around databinding in WPF and
Morning I am trying to return the distinct dates of an outcome by a
morning I have some doubts about the the way php works. I cant find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.