Question

In: Computer Science

There are two text files, whose names are given by two String variables, file1 and file2....

There are two text files, whose names are given by two String variables, file1 and file2. These text files have the same number of lines. Write a sequence of statements that creates a new file whose name consists concatenating the names of the two text files (with a "-" in the middle) and whose contents of merging the lines of the two files. Thus, in the new file, the first line is the first line from the first file, the second line is the first line from the second file. The third line in the new file is the second line in the first file and the fourth line is the second line from the second file, and so on. When finished, make sure that the data written to the new file has been flushed from its buffer and that any system resources used during the course of running your code have been released.(Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

Solutions

Expert Solution

We can answer the above question using the below code.

Code:  

File f1 = new File();

f1.File(file1);

File f2 = new File();

f2.File(file2);

Scanner ob1 = new Scanner(f1);

Scanner ob2 = new Scanner(f2);

String File3 = file1 + "-" + file2;  

File files = new File(File3);

PrintWriter ob = new PrintWriter();

ob. PrintWriter(file3);

while(ob1.hasNextLine())  

{

System.out.println(ob1.nextLine());  

System.out.println(ob2.nextLine());

}

output.close();

output.close();

Explanation:

  1. In the above java code firstly we create the File class object.
  2. Then we call the parameterized constructor of this class.
  3. Then we create the scanner class object.
  4. In the input time, we pass the parameter to the scanner class object and we declare the String variable that is File3.
  5. In this variable, we add both file1 and file2 value with concatenating "-".
  6. Then we create the PrintWriter class object and call their constructor.
  7. At the last, we declare the loop that prints all the values.



Related Solutions

Write a bash script to download two files, say file1.gz and file2.gz from a given web...
Write a bash script to download two files, say file1.gz and file2.gz from a given web address to the directory "assignment5"
Create two files, file1.csv and file2.csv Write the following information in file1.csv: Apple 1.69 001 Banana...
Create two files, file1.csv and file2.csv Write the following information in file1.csv: Apple 1.69 001 Banana 1.39 002 Write the following information in file2.csv: Apple 1.69 001 Carrot 1.09 003 Beef 6.99 004 You have two files, both of the two files have some information about products: • Read these two files, find out which products exist in both files, and then save these products in a new file. You can use the two files you created in Lab 1...
For this exercise, you will receive two string inputs, which are the names of the text...
For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully written to outFileName."...
Display nonduplicate names in ascending order) Given one or more text files, each representing a day’s...
Display nonduplicate names in ascending order) Given one or more text files, each representing a day’s attendance in a course and containing the names of the students who attended the course on that particular day, write a program that displays, in ascending order, the names of those students who have attended at least one day of the course. The text file(s) is/are passed as command-line argument(s). USe language java, ide netbeans
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
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...
Let X and Y be two discrete random variables whose joint probability distribution function is given...
Let X and Y be two discrete random variables whose joint probability distribution function is given by f(x, y) = 1 12 x 2 + y for x = −1, 1 and y = 0, 1, 2 a) Find cov(X, Y ). b) Determine whether X and Y are independent or not. (Justify your answer.
Write a program to remove extra blanks from text files. Your program should replace any string...
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows: * Create a temporary file. * Copy from the input file to the temporary file, but do not copy extra blanks. * Copy the contents of the temporary file back into the original file. * Remove the temporary file. Your temporary file must have a different name than any existing...
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...
Suppose a file (whose name is given through an argument by the user) contains student names...
Suppose a file (whose name is given through an argument by the user) contains student names and their scores (ranging from 0 to 100): i.e. each line contains a student name (just the first name with no space inside) and the student’s score separated by a space. Find out who has the highest score with what score, and who has the lowest score with what score. Also, calculate the average of all of the scores. Do not use arrays to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT