How would you write a java function boolean sameContent(Path file1,Path file2)
which determines if the two given paths point to files which store the same content? Of course, first, I would check if the file sizes are the same. This is a necessary condition for storing the same content. But then I'd like to listen to your approaches. If the two files are stored on the same hard drive (like in most of my cases) it's probably not the best way to jump too many times between the two streams.
Probably the easiest way to compare two files is to use the diff command. The output will show you the differences between the two files. The < and > signs indicate whether the extra lines are in the first (<) or second (>) file provided as arguments. In this example, the extra lines are in backup.
From the Micro Focus Data File Tools window, click Tools > Compare Files. The File Compare dialog box appears. and select the required file.
Exactly what FileUtils.contentEquals
method of Apache commons IO does and api is here.
Try something like:
File file1 = new File("file1.txt"); File file2 = new File("file2.txt"); boolean isTwoEqual = FileUtils.contentEquals(file1, file2);
It does the following checks before actually doing the comparison:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With