Question

Program using java !! Create two files, file1.csv and file2.csv Write the following information in file1.csv:...

Program using java !!

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

Homework Answers

Answer #1

Java.io.PrintWriter class

this class is used to  Prints formatted representations of objects in a text-output stream.

used to write line from other file as a filestream.

Java.io.BufferedReader Class in Java

Reads text from a character-input stream as a buffering characters so as to provide as a text reading in a line mod

code

//file1.csv contains data 
/*  Apple 1.69 001
    Banana 1.39 002   */
//file2.csv contains data 
/*
Apple 1.69 001
Carrot 1.09 003
Beef 6.99 004
*/    
//file3.csv contains data 
/*
Apple 1.69 001
*/
//file name check.java 
import java.io.*; 
public class check
{ 
  public static void main(String[] args)throws Exception 
  { 
  //BufferedReader to read file as a stream
  //br.readLine is ude to read line from file as string 
 BufferedReader br1 = new BufferedReader(new FileReader("file1.csv")); 
  BufferedReader br2 = new BufferedReader(new FileReader("file2.csv")); 
  String st; 
  //print file1 data 
  System.out.println("---------------------");
   System.out.println("File1 contains DATA : ");
   System.out.println("---------------------");
  while ((st = br1.readLine()) != null) 
    System.out.println(st); 
    //print file2 data 
   System.out.println("---------------------");
   System.out.println("File2 contains DATA : ");
   System.out.println("---------------------");
    while ((st = br2.readLine()) != null) 
    System.out.println(st); 
    BufferedReader br11 = new BufferedReader(new FileReader("file1.csv")); 
  BufferedReader br22 = new BufferedReader(new FileReader("file2.csv"));
    String line1,line2;
    //   PrintWriter to create new file as file3.txt
    PrintWriter pw = new PrintWriter("file3.csv");
    line1 = br11.readLine();
    line2= br22.readLine();
    System.out.println("---------------------");
   System.out.println("File3 contains DATA : ");
   System.out.println("---------------------");
   //code to check line1 and line2 have same string 
   //line1.equals(line2) return true if they are the same string
    while(line1!=null && line2!=null)
    {
    if(line1.equals(line2))
    {System.out.println(line1);
    pw.println(line1);
    } 
    line1 = br11.readLine();
    line2= br22.readLine();
    }
     pw.flush(); 
          
        // closing resources 
        br11.close(); 
         br22.close(); 
         br1.close(); 
         br2.close(); 
        pw.close();
  }
  }
  

Steps to run

Step 1 : write file name check.java

step2 : type javac check.java in the terminal for compile

step3 : type java check to run the file

Note

avoid writing extra line as null values means write line up to which required line don't give extra line while creating

your file file1.csv and file2.csv other wise it will givw error as null pointer exception.

output is below;

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and Jelly Sandwiches. The program must create two sandwiches based on user input. The sandwich information for both must then print out their details and determine if the two sandwiches are equal. Requirements: Write a class called Bread with the following Instance Variables Name: The name brand of the bread. o   Calories: The number of calories per slice assumed to be between 50 and 250 inclusively....
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...