Question

In: Computer Science

C# Simple Text File Merging Application - The idea is to merge two text files located...

C# Simple Text File Merging Application - The idea is to merge two text files located in the same folder to create a new txt file (also in the same folder). I have the idea, and I can merge the two txt files together, however I would like to have an empty line between the two files. How would I implement this in the code below?

if (File.Exists(fileNameWithPath1))
{

try
{
string[] file1 = File.ReadAllLines(fileNameWithPath1);
string[] file2 = File.ReadAllLines(fileNameWithPath2);

//dump file, I clear and save it after each run
using (StreamWriter FileWriter = File.CreateText(@"//Destination"))
{
for(int i = 0; i < file1.Length || i < file2.Length; i++)
{
if (i < file1.Length)
FileWriter.WriteLine(file1[i]);
if (i < file1.Length)
FileWriter.WriteLine(file2[i]);


}
Console.WriteLine("File Added Succesfully");

Solutions

Expert Solution

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;


namespace MergeProject
{

public class Program
{
public static void Main(string[] args)
{

string folderPath = @"C:\MergeFolder";
string firstFile = "firstFile.txt";
string secondFile = "secondFile.txt";
string targetFile = "targetFile.txt";
mergeTwoFiles(folderPath, firstFile, secondFile, targetFile);
Console.ReadLine();
}
      
       public static void mergeTwoFiles(string folderPath, string firstFile, string secondFile, string targetFile)
{

try
{
string firstFileFullPath = Path.Combine(folderPath, firstFile);
string secondFileFullPath = Path.Combine(folderPath, secondFile);
string targetFileFullPath = Path.Combine(folderPath, targetFile);

//check If directory exist
if (!Directory.Exists(folderPath))
{
Console.WriteLine("The directory {0} does not exist", folderPath);
return;
}

//remove existing Target File if it exist
if (File.Exists(targetFileFullPath))
{
File.Delete(targetFileFullPath);

}
//create new TargetFile
using (File.Create(targetFileFullPath))
{

}


string[] firstFileContent = new string[] { };

//check if file Exist and load to array [first File Contents]
if (File.Exists(firstFileFullPath))
{
firstFileContent = File.ReadAllLines(firstFileFullPath);
}
//check if file Exist and load to array
using (StreamWriter FileWriter = new StreamWriter(targetFileFullPath))
{
for (int i = 0; i < firstFileContent.Length; i++)
{
FileWriter.WriteLine(firstFileContent[i]);
}
}


//check if file Exist and load to array [second file Content]
string[] secondFileContent = new string[] { };
if (File.Exists(Path.Combine(folderPath, secondFile)))
{
secondFileContent = File.ReadAllLines(secondFileFullPath);
}

//check if file Exist and load to array
using (StreamWriter FileWriter = new StreamWriter(targetFileFullPath, true))
{

//check if new line is needed
if (firstFileContent.Length > 0 && secondFileContent.Length > 0)
{
FileWriter.WriteLine(); // Adding new line after a file Read
}

//add content of second file
for (int i = 0; i < secondFileContent.Length; i++)
{
FileWriter.WriteLine(secondFileContent[i]);
}
}
Console.WriteLine("File Added Succesfully");
}
catch (Exception)
{
Console.WriteLine("File Merge Operation Eror");
throw;
}

  
}
      
   }
}  


----input File--

firstFile.txt
12345678
6374739
938493
34
37488


secondFile.txt
ABC
DEF      


--------output File------------
targetFile.txt  
12345678
6374739
938493
34
37488

ABC
DEF  


Related Solutions

Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first...
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first is a text file that contains a template letter. template.txt [ Dear <>, Because you are <> years old and <>, we have a free gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $9.99. To claim your gift, call us immediately. Thank you, Office of Claims Department ] The tags <>, <>, and <> are...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Create a simple C++ application that will exhibit concurrencyconcepts. Your application should create two threads...
Create a simple C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0. For your created code, provide a detailed analysis of appropriate concepts that could impact your application. Specifically, address:Performance issues with concurrencyVulnerabilities exhibited with use of stringsSecurity of the data types exhibited.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Develop a simple MIS (Management Information System) that consists of a simple database (a text file)....
Develop a simple MIS (Management Information System) that consists of a simple database (a text file). The system manages to dynamically input record/data into the database. The data from the database can be sorted, searched and updated. User also should be able to add new records/data, remove any data and etc. Here are some ideas of MIS that can be developed: 1. Hotel reservation system. 2. Students management system. 3. Payroll management system. 4. Bus/Railway/Plane ticketing system. 5. Clinic record...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT