Qatam
Qatam

Reputation: 19

How to compare elements in two txt files and merge them into one file in java?

I have an already existed two text files that I want to take the similar data from them and merge this new data into one file.

For example:

//info1.txt
ID,Name,LastName,WorkPlace
12345,James,Jone,Us
34563,Mike,Smith,Canada
34523,Matt,jk,Uk

//info2.txt
ID,Proj1,Proj2,Proj3,Assignment,Final
34563,60,100,75,89,50
34523,70,56,75,100,70
12345,100,90,75,89,100

I was able to copy the information of these two text files into one text file, but couldn't compare each element in each line by the alternative in the other file!

My Code:

    import java.io.*; 

    public class Merge { 
    public static void main(String[] args) throws IOException { 

    //PrintWriter object for info3.txt 
    PrintWriter pw = new PrintWriter("info3.txt"); 

    //BufferedReader object for info1.txt & info2.txt
    BufferedReader br1 = new BufferedReader(new FileReader("info1.txt")); 
    BufferedReader br2 = new BufferedReader(new FileReader("info2.txt")); 

        String line1 = br1.readLine(); 
        String line2 = br2.readLine(); 

        //loop to copy each lines of the two files to info3.txt
        while (line1 != null || line2 !=null) { 
        if(line1 != null) { 
            pw.println(line1); 
            line1 = br1.readLine(); 
        }   
        if(line2 != null) { 
            pw.println(line2); 
                line2 = br2.readLine(); 
        } 
    } 
    pw.flush(); 
    br1.close(); 
    br2.close(); 
    pw.close(); 
    } 
    } 

This is what I'm looking for as a result: (Note: "P_Average" is the average of the three Projs in file "info2.txt").

//merged_file.txt
ID,Name,P_Average,Assignment,Final,WorkPlace
12345,James,88.3,89,100,Us
34523,Matt,67.0,100,70,Uk
34563,Mike,78.3,89,50,Canada

Upvotes: 0

Views: 798

Answers (1)

guleryuz
guleryuz

Reputation: 2734

by using java 8's Files api,

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Merge {

    public static void main(String[] args) throws IOException {

        List<String> students = Files.readAllLines(Paths.get("info1.txt"));
        List<String> grades = Files.readAllLines(Paths.get("info2.txt"));

        List<String> results = new ArrayList<>();

        results.add("ID,Name,P_Average,Assignment,Final,WorkPlace");

        // remove header lines
        students.remove(0);
        grades.remove(0);

        for(String student : students) {
            String[] s = student.split(",");
            for(String grade : grades) {
                String[] g = grade.split(",");
                if(Objects.equals(s[0], g[0])) {
                    results.add(s[0] + "," + s[1] + "," + ((Integer.parseInt(g[1]) + Integer.parseInt(g[2]) + Integer.parseInt(g[3]))/3) + "," + g[4] + "," + g[5] + "," + s[3]);
                }
            }
        }

        Files.write(Paths.get("info3.txt"), results);

    }
}

another simple way by using a data class (Student) and a data structure (Map)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class Merge {

    static class Student {
        String ID,Name,LastName,WorkPlace,Proj1,Proj2,Proj3,Assignment,Final;

        @Override
        public String toString() {
            return ID + "," + Name + "," + ((Integer.parseInt(Proj1) + Integer.parseInt(Proj2) + Integer.parseInt(Proj3))/3) + "," + Assignment + "," + Final + "," + WorkPlace;
        }
    }

    public static void main(String[] args) throws IOException {

        // PrintWriter object for info3.txt
        PrintWriter pw = new PrintWriter("info3.txt");

        // BufferedReader object for info1.txt & info2.txt
        BufferedReader br1 = new BufferedReader(new FileReader("info1.txt"));
        BufferedReader br2 = new BufferedReader(new FileReader("info2.txt"));

        // skip header lines
        br1.readLine();
        br2.readLine();

        Map<String, Student> map = new HashMap<>();

        String line;
        while((line = br1.readLine()) != null) {
            String[] data = line.split(",");
            Student s = new Student();
            s.ID = data[0];
            s.Name = data[1];
            s.LastName = data[2];
            s.WorkPlace = data[3];
            map.put(s.ID, s);
        }


        while((line = br2.readLine()) != null) {
            String[] data = line.split(",");
            Student s = map.get(data[0]);
            s.Proj1 = data[1];
            s.Proj2 = data[2];
            s.Proj3 = data[3];
            s.Assignment = data[4];
            s.Final = data[5];
        }

        pw.println("ID,Name,P_Average,Assignment,Final,WorkPlace");

        map.values().forEach( pw::println );

        pw.flush();
        br1.close();
        br2.close();
        pw.close();
    }
}

Upvotes: 1

Related Questions