Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging 2 Text Files in C#

Tags:

c#

file

merge

Firstly, i'd just like to mention that I've only started learning C# a few days ago so my knowledge of it is limited.

I'm trying to create a program that will parse text files for certain phrases input by the user and then output them into a new text document.

At the moment, i have it the program searching the original input file and gathering the selected text input by the user, coping those lines out, creating new text files and then merging them together and also deleting them afterwards.

I'm guessing that this is not the most efficient way of creating this but i just created it and had it work in a logical manor for me to understand as a novice.

The code is as follows;

private void TextInput1()
    {
        using (StreamReader fileOpen = new StreamReader(txtInput.Text))
        {
            using (StreamWriter fileWrite = new StreamWriter(@"*DIRECTORY*\FIRSTFILE.txt"))
            {
                string file;
                while ((file = fileOpen.ReadLine()) != null)
                {
                    if (file.Contains(txtFind.Text))
                    {
                            fileWrite.Write(file + "\r\n");
                    }
                }
            }
        }
    }

    private void TextInput2()
    {
        using (StreamReader fileOpen = new StreamReader(txtInput.Text))
        {
            using (StreamWriter fileWrite = new StreamWriter(@"*DIRECTORY*\SECONDFILE.txt"))
            {
                string file;
                while ((file = fileOpen.ReadLine()) != null)
                {
                    if (file.Contains(txtFind2.Text))
                    {
                        fileWrite.Write("\r\n" + file);
                    }
                }
            }
        }
    }

    private static void Combination()
    {
        ArrayList fileArray = new ArrayList();

        using (StreamWriter writer = File.CreateText(@"*DIRECTORY*\FINALOUTPUT.txt"))
        {

            using (StreamReader reader = File.OpenText(@"*DIRECTORY*\FIRSTFILE.txt"))
            {
                writer.Write(reader.ReadToEnd());
            }

            using (StreamReader reader = File.OpenText(@"*DIRECTORY*\SECONDFILE.txt"))
            {
                writer.Write(reader.ReadToEnd());
            }
        }

    }

    private static void Delete()
    {
        if (File.Exists(@"*DIRECTORY*\FIRSTFILE.txt"))
            {
                File.Delete(@"*DIRECTORY*\FIRSTFILE.txt");
            }

        if (File.Exists(@"*DIRECTORY*\SECONDFILE.txt"))
            {
                 File.Delete(@"*DIRECTORY*\SECONDFILE.txt");
            }

    }

The output file that is being created is simply outputting the first text input followed by the second. I am wondering if it is possible to be able to merge them into 1 file, 1 line at a time as it is a consecutive file meaning have the information from Input 1 followed 2 is needed rather than all of 1 then all of 2.

Thanks, Neil.

like image 570
Sildeh Avatar asked Dec 09 '22 19:12

Sildeh


1 Answers

To combine the two files content in an one merged file line by line you could substitute your Combination() code with this

string[] file1 = File.ReadAllLines("*DIRECTORY*\FIRSTFILE.txt");
string[] file2 = File.ReadAllLines("*DIRECTORY*\SECONDFILE.txt");

using (StreamWriter writer = File.CreateText(@"*DIRECTORY*\FINALOUTPUT.txt"))
{
    int lineNum = 0;
    while(lineNum < file1.Length || lineNum < file2.Length)
    {
        if(lineNum < file1.Length)
            writer.WriteLine(file1[lineNum]);
        if(lineNum < file2.Length)
            writer.WriteLine(file2[lineNum]);
        lineNum++;
    }
}

This assumes that the two files don't contains the same number of lines.

like image 114
Steve Avatar answered Dec 11 '22 08:12

Steve