2
votes

I have two related text files shown for example in data1.txt and data2.txt. I want to merge the two files to create result.txt. Any idea how to go about this?

data1.txt

books, 3
Shelf, 5
groceries,6
books, 1
Shelf, 2

data2.txt

books,2
shelf,3
groceries,1

result.txt

books, 3, 2
Shelf, 5,3
groceries,6,1
books, 1,2
Shelf, 2, 3
3
read line one by one from both ,then split finally combine - Madhawa Priyashantha
I tried reading line by line but it does not work because the order of data2 is not the same as data1 - Free Man
why 4th line of result.txt is "books, 1,2" ?? - Madhawa Priyashantha
the corresponding value for books in data2.txt is 2. - Free Man
I had a project just like this a couple weeks ago, sure I can help. Question tho, I notice that some of these arrangements are a bit different, I see that some of the commas have spaces after them, others don't, is that a mistake or is that what we should expect? - DreadHeadedDeveloper

3 Answers

1
votes

this is a example for you.first you need to add values to 2d list from data2 text file.and then when line is null in file2 you can get mapping value relative to it's text from that list .so i have a method which will return back the mapping value for a String .code is little long than i thought .i post only relevant methods here.This is link to complete class file

public void marged(){
    try {
        BufferedReader br1 = null;
        BufferedReader br2 = null;

        String line1;
        String line2;
        ArrayList<ArrayList<String>> arrayList = new ArrayList<>();

        br1 = new BufferedReader(new FileReader("C:\\Users\\Madhawa.se\\Desktop\\workingfox\\data1.txt"));
        br2 = new BufferedReader(new FileReader("C:\\Users\\Madhawa.se\\Desktop\\workingfox\\data2.txt"));

        while ((line1 = br1.readLine()) != null) {

            String[] split1 = line1.split(",");

            String line1word = split1[0].trim();
            String line1val = split1[1].trim();

            line2 = br2.readLine();

            if (line2 != null) {
                String[] split2 = line2.trim().split(",");

                String line2word = split2[0].trim();
                String line2val = split2[1].trim();

                ArrayList<String> list = new ArrayList();
                list.add(line2word);
                list.add(line2val);
                arrayList.add(list);

                if (line1word.equalsIgnoreCase(line2word)) {
                    String ok = line1word + "," + line1val + "," + line2val;
                    System.out.println(ok);
                }

            } else {
                String ok = line1word + "," + line1val + "," + doesexist(arrayList, line1word);
                System.out.println(ok);
            }

        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

this is the method return mapping value

public String doesexist(ArrayList<ArrayList<String>> arrayList, String s) {

    for (int i = 0; i < arrayList.size(); i++) {
        String get = arrayList.get(i).get(0);
        if (get.trim().equalsIgnoreCase(s.trim())) {
            return arrayList.get(i).get(1);
        }
    }
    return "-1";
 }

output>>

books,3,2
Shelf,5,3
groceries,6,1
books,1,2
Shelf,2,3
0
votes

Simply add files into an array of File object then read it using loop.

File []files = new Files[amountOfFiles];
//initialize array elements
for(File f:files)
{
      //read each file and store it into string variable
}

//finally write the string variable into result.txt file.
0
votes
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class SOQ21
{

   public SOQ21()
   {

      merge();

   }

   public void merge()
   {

      try
      {
         String firstfile = "data1.txt";
         FileReader fr1 = new FileReader(firstfile);
         BufferedReader bfr1 = new BufferedReader(fr1);

         String secondfile = "data2.txt";
         FileReader fr2 = new FileReader(secondfile);
         BufferedReader bfr2 = new BufferedReader(fr2);

      /*
      ^^^ Right here is how you get the files and accompanying BufferedReaders 
      to handle them
      */

      //next, using the readLine() method from the Java API, read each line
      //for the first file

      //then, separate by taking the words into an ArrayList and storing the
      //numbers as Strings in a String[] of equal length of the ArrayList


      //Do the same for the second file

      //Then, if the word of ArrayList 1 matches the word of ArrayList 2, 
      //append the String numbers from String[] 2 to String[] 1

      //DONE! :)

      }

      catch(FileNotFoundException ex)
      {

      //handle how you want

      }


   }

   public static void main(String[] args)
   {

      SOQ21 soq = new SOQ21();

   }

}

The comments I made should answer most of your questions. Lastly, I would pay special attention to the exceptions, I'm not entirely sure how you wanted to deal with that, but make sure you fill it with SOMETHING!