-5

I have two files say

abc           
cdg
sfh
drt
fgh

and another file

ahj
yuo
jkl
uio
abc
cdg

I want to compare these two files and get output file as

abc
cdg
sfh
drt
fgh
ahj
yuo
jkl
uio

this is my code

public static void MergeFiles(final File priviousModifiedFilesList, final File currentModifiedFilesList,
      final File ModifiedFilesList) {
    FileWriter fstream = null;
    out = null;
    try {
      fstream = new FileWriter(ModifiedFilesList, true);
      out = new BufferedWriter(fstream);
    }
    catch (IOException e1) {
      e1.printStackTrace();
    }

    System.out.println("merging: " + priviousModifiedFilesList + "\n");
    System.out.println("merging: " + currentModifiedFilesList);
    FileInputStream fis1;
    FileInputStream fis2;
    try {
      fis1 = new FileInputStream(priviousModifiedFilesList);
      BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(fis1));

      fis2 = new FileInputStream(currentModifiedFilesList);
      BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(fis2));

      String Line1;
      String Line2;

      while (((Line1 = bufferedReader1.readLine()) != null)) {

        while ((Line2 = bufferedReader2.readLine()) != null) {

          if (Line1.equals(Line2)) {
            out.write(Line1);
          }

          out.write(Line2);
          out.newLine();
        }
         out.write(Line1);
      }
      bufferedReader1.close();
      bufferedReader2.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    out.close();
}

it writes all the lines from first file and when the lines match it stops.

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
  • 3
    And where is the problem? What have you tried so far? – Jens Aug 27 '14 at 07:59
  • Stack Overflow is for asking programming questions about code you are writing. Currently you've written a set of requirements, which is more appropriate for a contractor-hiring web site. Please show us what you've attempted and where you got stuck. That being said, even your requirements are unclear - it seems like you want to remove duplicates from the merged file? – Duncan Jones Aug 27 '14 at 07:59
  • possible duplicate of [concatenating a number of .txt files in java](http://stackoverflow.com/questions/10675450/concatenating-a-number-of-txt-files-in-java) – xlecoustillier Aug 27 '14 at 07:59
  • @X.L.Ant I don't think that's a duplicate, because there seems to be some requirement here for de-duplication of output. – Duncan Jones Aug 27 '14 at 08:01
  • 1
    take a look this answer http://stackoverflow.com/a/5283123/1214847 – zapdroid Aug 27 '14 at 08:01
  • @Duncan yeah, you're right. My bad. Still NARQ though. – xlecoustillier Aug 27 '14 at 08:03
  • @X.L.Ant Agreed. I've voted to close as "unclear what you're asking", since the requirements are unclear, as is the current progress made. – Duncan Jones Aug 27 '14 at 08:03

1 Answers1

0

It's easy:

  • Read you first file line by line (you can use a Scanner for that).
  • For each line, write it to the output file (you can use a PrintWriter for that).
  • Also store the line in a HashSet.

  • Read your second file line by line.
  • For each line, check if the line is in the HashSet.
  • If it's not, write it to the output file.

  • Close your files.
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47