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

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...
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...
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.
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...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks. Instead of displaying the results on the screen, send the results to an output named using your last name. Example:       Contents of Exercise13_3.txt: 100 95 88 97 71 67 80 81 82             Contents of YourLastName.txt: Your...
Develop an application that searches for files on a wild card basis (a*.c, .c, *a, etc)...
Develop an application that searches for files on a wild card basis (a*.c, .c, *a, etc) and then displays the resultant file names in all CAPITALS using pipes in a "multiprocess setup" C language must be used
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last...
Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last name, and phone number as the example: MIKEL BUETTNER 3545044 ENOCH BUGG 2856220 BRENDON LAVALLEY 433312 QUINTIN CREEK 5200413 JAMISON MILLETT 6243458 FLORENCIO PUMPHREY 3296862 DARRICK FREGOSO 6868442 TOBIAS GLASSMAN 6040564 and then ask the user to add a new contact first name, last name, and phone number and same the information into a file. Use array and pointer
Which of the following files is a temporary file? a. transaction file b. master file c. reference file d. none of the above
Which of the following files is a temporary file? a. transaction fileb. master filec. reference filed. none of the above
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT