title: Compare 2 text files in java and write the difference in both separately into another file
body: <p>This is the code I have written to perform a validation mechanism for comparing 2 files. 
  I want to know is there a way to write it in a more performing way, because both of my files can have millions of records in it and this I believe will be slow in those cases.</p>
  
  <p>I am thinking of adding a Hash map, every time I get occurrence of a line in the file, I will add +1 to key value. If not, the value of the key remains 1.
  If the record exists in the other map of file 2 , then I remove it from first map. 
  If it doesn't, then I add it to the map.
  This goes alternation files till end. </p>
  
  <p><strong>I don't do a line by line comparison because order of the lines may be different in both files.</strong></p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      BufferedReader br1 = null;
      BufferedReader br2 = null;
      BufferedWriter br3 = null;
      String sCurrentLine;
      int linelength;
      List&lt;String&gt; list1 = new ArrayList&lt;String&gt;();
      List&lt;String&gt; list2 = new ArrayList&lt;String&gt;();
      List&lt;String&gt; unexpectedrecords = new ArrayList&lt;String&gt;();
  
      br1 = new BufferedReader(new FileReader("expected.txt"));
  
      br2 = new BufferedReader(new FileReader("actual.txt"));
  
      while ((sCurrentLine = br1.readLine()) != null) {
          list1.add(sCurrentLine);
      }
      while ((sCurrentLine = br2.readLine()) != null) {
          list2.add(sCurrentLine);
      }
      List&lt;String&gt; expectedrecords = new ArrayList&lt;String&gt;(list1);
  
      List&lt;String&gt; actualrecords = new ArrayList&lt;String&gt;(list2);
  
      if (expectedrecords.size() &gt; actualrecords.size()) {
          linelength = expectedrecords.size();
      } else {
          linelength = actualrecords.size();
      }
  
      for (int i = 0; i &lt; linelength; i++) {
          if (actualrecords.contains(expectedrecords.get(i))) {
              actualrecords.remove(expectedrecords.get(i));
          } else {
              unexpectedrecords.add(actualrecords.get(i));
          }
      }
  
      br3 = new BufferedWriter(new FileWriter(new File("c.txt")));
      br3.write("Records which are not present in actual");
      for (int x = 0; x &lt; unexpectedrecords.size(); x++) {
          br3.write(unexpectedrecords.get(x));
          br3.newLine();
      }
      br3.write("Records which are in actual but no present in expected");
      for (int i = 0; i &lt; actualrecords.size(); i++) {
          br3.write(actualrecords.get(i));
          br3.newLine();
      }
      br3.flush();
      br3.close();
  }
  </code></pre>