?
Solved

Split a text file into multiple files using VB.NET or C#

Posted on 2014-08-19
2
Medium Priority
?
3,230 Views
Last Modified: 2016-02-17
I have a file that I need to split based on the first field in the file.

For example

1|1000
1|1000
2|1000
2|1000
3|10000
1|10000

I would like this to create 3 files based on the first field in the file.
0
Comment
Question by:aothomas42
  • 2
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40271622
Here's one approach:

using System.Collections.Generic;
using System.IO;

namespace _28501266
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, StreamWriter> outputWriters = new Dictionary<string, StreamWriter>();

            using (StreamReader reader = new StreamReader(@"C:\path\to\file.txt"))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string firstColumn = line.Substring(0, line.IndexOf('|'));
                    StreamWriter writer;

                    if (outputWriters.ContainsKey(firstColumn))
                    {
                        outputWriters.Add(firstColumn, new StreamWriter(@"C:\path\to\output\file" + firstColumn + ".txt"));
                    }

                    writer = outputWriters[firstColumn];
                    writer.WriteLine(line);
                }
            }

            foreach (string key in outputWriters.Keys)
            {
                outputWriters[key].Close();
            }
        }
    }
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40271626
And a LINQ-based approach should you prefer:

using System.IO;
using System.Linq;

namespace _28501266
{
    class Program
    {
        static void Main(string[] args)
        {
            var query = from line in File.ReadLines(@"C:\path\to\file.txt")
                        let firstColumn = line.Substring(0, line.IndexOf('|'))
                        group line by firstColumn into outputs
                        select outputs;

            foreach (var output in query)
            {
                File.WriteAllLines(@"C:\path\to\output\file" + output.Key + ".txt", output);
            }
        }
    }
}

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question