Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Reading and writing the files with Java can be just as fast. You can use <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileChannel.html" rel="noreferrer">FileChannels</a>. As for comparing the files, obviously this will take a lot of time comparing byte to byte Here's an example using FileChannels and ByteBuffers (could be further optimized):</p> <pre><code>public static boolean compare(String firstPath, String secondPath, final int BUFFER_SIZE) throws IOException { FileChannel firstIn = null, secondIn = null; try { firstIn = new FileInputStream(firstPath).getChannel(); secondIn = new FileInputStream(secondPath).getChannel(); if (firstIn.size() != secondIn.size()) return false; ByteBuffer firstBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE); ByteBuffer secondBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE); int firstRead, secondRead; while (firstIn.position() &lt; firstIn.size()) { firstRead = firstIn.read(firstBuffer); secondRead = secondIn.read(secondBuffer); if (firstRead != secondRead) return false; if (!buffersEqual(firstBuffer, secondBuffer, firstRead)) return false; } return true; } finally { if (firstIn != null) firstIn.close(); if (secondIn != null) firstIn.close(); } } private static boolean buffersEqual(ByteBuffer first, ByteBuffer second, final int length) { if (first.limit() != second.limit()) return false; if (length &gt; first.limit()) return false; first.rewind(); second.rewind(); for (int i=0; i&lt;length; i++) if (first.get() != second.get()) return false; return true; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload