title: Read and compare two large Files
body: <p>I would like to read and compare all the lines of both files, I explain, I would like to find for each password hasher (from my test.txt file) the hashes that are the same (from the password.txt file). The problem is that it should be fast enough (I would say max 45 min for 10M for password.txt and 1M for test.txt).</p>
  <p>I have for the moment this code</p>
  <pre class="lang-java prettyprint-override"><code>private static void bufferedReaderFilePasswordFirst() {
      Path path = Paths.get(&quot;C:\\Users\\basil\\OneDrive - Haute Ecole Bruxelles Brabant (HE2B)\\Documents\\NetBeansProjects\\sha256\\passwords.txt&quot;);
      Path pathUser = Paths.get(&quot;C:\\Users\\basil\\OneDrive - Haute Ecole Bruxelles Brabant (HE2B)\\Documents\\NetBeansProjects\\sha256\\test.txt&quot;);
      int nbOfLine = 0;
      StringBuffer oui = new StringBuffer();
  
      try (BufferedReader readerPasswordGenerate = Files.newBufferedReader(path, Charset.forName(&quot;UTF-8&quot;));) {
  
          String currentLineUser = null;
          String currentLinePassword = null;
  
          long start = System.nanoTime();
  
          while (((currentLinePassword = readerPasswordGenerate.readLine()) != null)) {
              BufferedReader readerPasswordUser = Files.newBufferedReader(pathUser, Charset.forName(&quot;UTF-8&quot;));
              while ((currentLineUser = readerPasswordUser.readLine()) != null) {
                  String firstWord = currentLinePassword.substring(0, currentLinePassword.indexOf(&quot;:&quot;));
                  if ((firstWord.charAt(0) == currentLineUser.charAt(0)) 
                      &amp;&amp; (firstWord.charAt(14) == currentLineUser.charAt(14)) 
                      &amp;&amp; (firstWord.charAt(31) == currentLineUser.charAt(31)) 
                      &amp;&amp; (firstWord.charAt(63) == currentLineUser.charAt(63))
                  ) {
                      if (firstWord.equals(currentLineUser)) {
                          String secondWord = currentLinePassword.substring(currentLinePassword.lastIndexOf(&quot;:&quot;) + 1);
  
                          oui.append(secondWord).append(System.lineSeparator());
                      }
                  }
              }
              if (nbOfLine % 300 == 0) {
                  System.out.println(&quot;We are at the &quot; + nbOfLine);
                  final long consumed = System.nanoTime() - start;
                  final long totConsumed = TimeUnit.NANOSECONDS.toMillis(consumed);
                  final double tot = (double) totConsumed;
                  System.out.printf(&quot;Not done. Took %s seconds&quot;, (tot / 1000));
                  System.out.println(oui + &quot; oui&quot;);
              }
              nbOfLine++;
          }
          System.out.println(oui);
          final long consumed = System.nanoTime() - start;
          final long totConsumed = TimeUnit.NANOSECONDS.toMillis(consumed);
          final double tot = (double) totConsumed;
          System.out.printf(&quot;Done. Took %s seconds&quot;, (tot / 1000));
      } catch (IOException ex) {
          ex.printStackTrace(); //handle an exception here
      }
  }
  </code></pre>
  <p>In this code, I just compare for each element in my <code>test.txt</code> if the corresponding element in the password hash is same.</p>
  <p>The <code>password.txt</code> contains for all elements: <code>hash:password</code>
  and <code>test.txt</code> contains only: <code>hash</code></p>
  <p>Thanks</p>