0

This question has already been asked 7 years ago but I can't add answers anymore because it's [closed].

So this is not a question - this is a solution suggestion : Since Java 1.7 this can be a one liner:

public class FilesComparator {
    public static boolean filesEquals(Path f1, Path f2) throws IOException {
        return Arrays.equals(Files.readAllBytes(f1), Files.readAllBytes(f2));
    }
}
Uebergeek
  • 29
  • 6
  • 1
    This will read ALL BYTES of BOTH files even if the first byte is different. Short but inperformant – Felix Sep 01 '17 at 09:53
  • 2
    If you want to add an answer, you should post a question and then post the solution as an answer. – Sweeper Sep 01 '17 at 09:54
  • 4
    I'm voting to close this question as off-topic because this is an answer to another question as stated by OP itself. – SpaceTrucker Sep 01 '17 at 09:59
  • Yes - for big files it's not suitable. I have to compare small swagger files. For big blobs I would also not use this. – Uebergeek Sep 01 '17 at 09:59
  • You can't answer the other [question](https://stackoverflow.com/q/1510520/85421) because it was was closed. Ideally you would write a (correct) question (re-edit this *question*) and add an answer to it! – user85421 Sep 01 '17 at 10:13

1 Answers1

1

How about this?

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FilesComparator {

    public static boolean isEquals(File f1, File f2) throws IOException {
        if (f1.length() != f2.length()) {
            return false;
        }

        try (InputStream f1Is = new BufferedInputStream(new FileInputStream(f1)); // or just new FileInputStream(f1) if you don't want to cache some bytes and want to make a native call for each byte
             InputStream f2Is = new BufferedInputStream(new FileInputStream(f2))) {

            int f1CurrentByte;
            int f2CurrentByte;
            while ((f1CurrentByte = f1Is.read()) != -1 && (f2CurrentByte = f2Is.read()) != -1) {
                if (f1CurrentByte != f2CurrentByte) {
                    return false;
                }
            }

            return true;
        }
    }

}
ruslanys
  • 1,183
  • 2
  • 13
  • 25
  • It was probably an answer in the existing question (not linked...) a byte per byte comparison, simple an efficient – AxelH Sep 01 '17 at 10:01
  • Non-buffered, single-byte reads from a FileInputStream is never a good idea. At least use BufferedInputStream and reasonable byte-arrays to read in chunks of data, otherwise the performance will be horrible. The equality of the data buffers can be checked with Arrays.equals like in the original "question". – Lothar Sep 01 '17 at 10:10
  • Using `BufferedInputStream` should be enough, no need to read in chunks. `BufferedInputStream` internally do the reading in chunks already. – Thariq Nugrohotomo Sep 01 '17 at 10:13
  • `BufferedInputStream` is faster - this is true, cos it's using buffer for 8KB by default. He can use the `FileInputStream` if he need to fetch byte by byte from native call, or `BufferedInputStream`, if it possible to cache some bytes. – ruslanys Sep 01 '17 at 10:23