Question

In: Computer Science

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 as an example. If you use file1.csv and file2.csv from Lab 1, the result file will be:

•Result file:

Apple 1.69 001

8.5.0

Solutions

Expert Solution

JAVA Program to read two files, finding similar products that exist in both files and save it in a new file :

import java.io.File;//Importing all required modules
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;

class JavaEg {
    public static void main(String[] args) throws IOException {
        
        Scanner input1 = new Scanner(new File("file1.csv"));//reading first file
        Scanner input2 = new Scanner(new File("file2.csv"));//reading second file

while(input1.hasNextLine() && input2.hasNextLine()){
    String first_file = input1.nextLine();   //reading lines on first file
    String second_file = input2.nextLine(); //reading lines on second file

    if(first_file.equals(second_file)){   //comparing lines of both files
        System.out.println("Similarities found: "+"\n"+first_file);
        String content = first_file+"\n";
        String path = "File3.csv"; //path of new output file
        try { //using try to write 
            Files.write(Paths.get(path), content.getBytes(), StandardOpenOption.APPEND); //writing similar contents to anew output file
        }catch (IOException e) { // using catch for exception
        }
    }
}
    }
}

File 1:

File 2:

Output File3:

The answer have been updated in Java language.


Related Solutions

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...
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"
python . The two csv files will contain lines in the following format: Angle, time, speed...
python . The two csv files will contain lines in the following format: Angle, time, speed - where each action will be specified as: • A, t, s = Move at an angle of A degrees with respect to East direction (positive horizontal axis) for t seconds with speed s meters per second.    The function should return 3 numpy arrays: • The expected horizontal displacements for each microcar • The expected vertical displacements for each microcar • The expected...
Please answer the following questions thank you!! 1) Assume file1.txt was created more recently than file2.txt....
Please answer the following questions thank you!! 1) Assume file1.txt was created more recently than file2.txt. You can test this using which of the following conditions? A) [ file1.txt –n file2.txt ] B) [ file1.txt –nt file2.txt ] C) [ file1.txt –ot file2.txt ] D) [ -newer file1.txt file2.txt ] E) None of these, there is no such test 2) We want to test to see if file1.txt is not readable. Which of these conditions will do so? A) [[...
Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
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++ Create an ArrayBag template class from scratch. This will require you to create two files:...
C++ Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation. The ArrayBag class must contain the following protected members: static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or...
Create files with the following names : test1, test2, test3. Create a symbolic link to test2...
Create files with the following names : test1, test2, test3. Create a symbolic link to test2 and name it test4 Create a directory and name it test5. Write a shell script to perform the following tasks: • check if a file named test6 exist. If not, it should create it. • Display a text to indicate whether test4 is symbolic link or not • Display a text to indicate whether test2 is directory or not • Display a text to...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the following three methods in the class RandomFile: ​public static void randomBinaryFile(String fileName, int length) ​public static void randomNumberFile(String fileName, intlength) ​public static void randomCharFile(String fileName, int length) The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file. ● randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length; ● randomNumberFile...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT