I have two properties files that contain information. I would like to diff them to see if they are the same. However, in properties files, unless you specify an order to output they write to a file in a different order. I don't have access to the code just these files. How can I check if their contents are the same?
For example,
File1 File2
a e
b c
c a
d d
e b
How can I detect that these two files would be the same? a-e represent strings of information
Thanks!
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.
You have already accepted an answer, but I'll add this anyway, just to point out that there's an easier way (assuming you are talking about normal Java properties files).
You actually don't have to do any sorting, line-by-line comparison etc. yourself, because equals() in java.util.Properties
has been implemented smartly and does what one would expect. In other words, "know and use the libraries", as Joshua Bloch would say. :-)
Here's an example. Given file p1.properties
:
a = 1
b = 2
and p2.properties
:
b = 2
a = 1
...you can just read them in and compare them with equals():
Properties props1 = new Properties();
props1.load(new FileReader("p1.properties"));
Properties props2 = new Properties();
props2.load(new FileReader("p2.properties"));
System.out.println(props1.equals(props2)); // true
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