In: Computer Science
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");
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