How can I distinguish two different text files without regard to line order?

I have two properties files containing information. I would like to separate them to see if they are the same. However, in property files, if you do not specify the output order, they write to the file in a different order. I do not have access to the code of these files. How can I check if their contents match?

For instance,

File1 File2 ae bc ca dd eb 

How can I determine that these two files will be the same? ae represent lines of information

Thanks!

+4
source share
4 answers

You've already accepted the answer, but I'll add this anyway, just point out that there is an easier way (assuming you're talking about normal Java property files).

You really don't need to do any sorting, linear comparison, etc. on its own, because equals () in java.util.Properties was implemented wisely and does what you would expect. In other words, β€œ know and use libraries, ” as Joshua Bloch would say. :-)

Here is an example. This p1.properties file:

 a = 1 b = 2 

and p2.properties :

 b = 2 a = 1 

... you can just read them 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 
+9
source

Read them, sort them, then skip them next to each other and compare them. Sorting can be done by inserting them into a sorted data structure, by the way.

+3
source

Sort the contents, and then compare line by line.

+1
source

You can do this on the command line. Your two files: File1 and File2.

$ sort File1> sorted_File1

$ sort File2> sorted_File2

And then use a diff utility such as meld (which is a graphical diff program).

$ meld sorted_File1 sorted_File2

-1
source

All Articles