Your answer doesn't really say why the OP's program doesn't work, you just say you can't reproduce the problem. Most likely, OP is calling compareFile inside a loop that never ends.
Add a comment. Kick Kick 4, 2 2 gold badges 18 18 silver badges 25 25 bronze badges. Above method by Jess will fail if file2 is same as file1 but has an extra line at the end. This should work.
Bikram Dhall Bikram Dhall 1. This is incredibly ineffective - reads the whole files into memory. Also, incorrect - containsAll does not check if the order is the same. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 4. Related Hot Network Questions. The proof in the pudding is eating it: The files have to be compared.
The file I wanted to copy was created to contain random bytes. Transferring only text information can sometimes leave tricky bugs lurking in the code. The random file was created using the simple Java class:. Using IntelliJ, comparing files is fairly easy, but since the files are binary and large, this approach is not really optimal.
I decided to write a short program that will not only signal that the files are different, but also where the difference is. The code is extremely simple:.
The running time comparing the two MB files is around 6 seconds on my SSD-equipped Mac Book — and it does not improve significantly if I specify a large, say 10MB, buffer as the second argument to the constructor of BufferedInputStream. On the other hand, if we do not use the BufferedInputStream , then the time is approximately ten times more. This is acceptable, but if I simply issue a diff sample. It can be many factors, like Java's startup time or code interpretation at the start of the while loop, until the JIT compiler thinks it is time to start to work.
My hunch is, however, that the code spends most of the time reading the file into the memory. Reading the bytes to the buffer is a complex process. It involves the operating system, the device drivers, the JVM implementation , moving bytes from one place to the other, and finally, we only compare the bytes, nothing else.
It can be done in a simpler way. We can ask the operating system to do it for us and skip most of the Java runtime activities, file buffers, and other glitters. We can ask the operating system to read the file to memory and then just fetch the bytes one by one from where they are.
We do not need a buffer, which belongs to a Java object and consumes heap space. We can use memory-mapped files. After all, memory-mapped files use Java NIO, and that is exactly the topic of the part of the tutorial videos that are currently in the making. Memory-mapped files are read into the memory by the operating system and the bytes are available to the Java program.
Scott Presnell Scott Presnell 1, 10 10 silver badges 23 23 bronze badges. If you only had a checksum for one of the files, this would be useful, but if you have both files on disk this is unnecessary. Isn't it sha1sum instead of sha1? There are two files that will return the same result despite being different: shattered. SHA1 has already one public collision shattered. One collision can be used to generate countless of colliding files Use SHA2 for hashing instead please. Show 1 more comment.
Meld also works with binary files when they aren't converted to hex first. It shows hex values for things which aren't in the char set, otherwise normal chars, which is useful with binary files that also contain some ascii text. Many do, at least begin with a magic string. Rikki Rikki 1, 14 14 silver badges 17 17 bronze badges. Can you explain your down votes please? SHA1 has 4 upvotes, and if the OP thinks there's a chance the two files could be the same or similar, the chances of a collision are slight and not worthy of down voting MD5 but up voting SHA1 other than because you heard you should hash your passwords with SHA1 instead of MD5 that's a different problem.
I downvoted because you posted a minor variant of an earlier bad solution, when it should have been a comment. The quickest way to check large files : Thanks a lot — Sumeet Patil. This is exactly what I found using URL to manual that you have provided. Victor Yarema, I don't know what you mean by "binary mode". The -b option merely prints the first byte that is different.
For finding flash memory defects, I had to write this script which shows all 1K blocks which contain differences not only the first one as cmp -b does! Daniel Alder Daniel Alder 4, 1 1 gold badge 43 43 silver badges 49 49 bronze badges. Please call the script using sh -x for debugging — Daniel Alder.
This is via calling the script from terminal. Line is 9. The script is ok. Please post your debug output to pastebin. You can see here what I mean: pastebin. Currently creating paste on pastebin. Show 3 more comments. DKroot DKroot 1, 13 13 silver badges 22 22 bronze badges. Try diff -s Short answer: run diff with the -s switch.
Long answer: read on below. Here's an example. Why is there no output?!? The answer is: this is by design. There is no output on identical files. Community Bot 1 1 1 silver badge. For instance, with this command: radiff2 -x file1. My favourite ones using xxd hex-dumper from the vim package : 1 using vimdiff part of vim! Michal Ambroz Michal Ambroz 2 2 bronze badges. Not quite. Only the possibility is high. What is the probability of failing?
Slim, but worse than using some variant of diff , over which there is no reason to prefer it.
0コメント