Search code examples
javapropertiescomparefile-comparison

COMPARE 2 or more .PROPERTIES files comparing ONLY keys


I want to compare 2 or more (if posible) .properties files, exactly are i18n files. So I have the default messages_es.properties where I first add the keys with values, what I really need is to compare only the keys of the default/primary messages_es.properties with other .properties file for example messages_en.properties, to know which translations are left on different .properties files.

Basically:

  • Input: Two properties files
  • Output: Missing keys on the 2nd .properties file

The O/P should show the keys missing on the 2nd .properties files.


Solution

  • Class Properties has methods

    public synchronized void load(InputStream inStream)
    public synchronized void load(Reader reader)
    

    You can use them to load your files.

    Then use method

    public Set<String> stringPropertyNames()
    

    To get set of properties.

    Finally Set has methods

    boolean retainAll(Collection<?> c)
    boolean removeAll(Collection<?> c)
    

    To work with difference.