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
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;
Get Answers For Free
Most questions answered within 1 hours.