Tutorial :Tool to find duplicate keys and value in properties file



Question:

is there a tool that tells me redundant keys and values that are there in my one or many properties file.


Solution:1

There is an Ant task, RscBundleCheck, that checks for the existence of duplicate keys in a set of resource files:

http://rscbundlecheck.sourceforge.net/

This would be simple way to integrate checking for duplicate properties into your build process.


Solution:2

/**   *  Purpose:  Properties doesn't detect duplicate keys.  So this exists.   *  @author shaned   */  package com.naehas.tests.configs;    import java.util.Properties;    import org.apache.log4j.Logger;    public class NaehasProperties extends Properties  {     private static final long   serialVersionUID = 1L;       private static final Logger log              = Logger.getLogger(NaehasProperties.class);       public NaehasProperties()     {        super();     }       /**      * @param defaults      */     public NaehasProperties(Properties defaults)     {        super(defaults);     }       /**      * Overriding the HastTable put() so we can check for duplicates      *       */     public synchronized Object put(Object key, Object value)     {        // Have we seen this key before?        //        if (get(key) != null)        {           StringBuffer message = new StringBuffer("Duplicate key found: " + key + " with value: " + value);           message.append(". Original value is: " + (String) get(key));             log.error(message.toString());             // Setting key to null will generate an exception and cause an exit.           // Can not change the signature by adding a throws as it's not compatible           // with HashTables put().           //           // If you commented out this line, you will see all the occurrences of the duplicate key           // as the put will overwrite the past encounter.           //           key = null;        }          return super.put(key, value);     }  }  


Solution:3

I don't know if there is an existing tool, but you should be able to write a short java program, or script in a language you are comfortable with that should do this in no time. Then you would also have it for future use.

A quick google search yielded the following http://www.javanb.com/netbeans/1/19793.html

this has a gui tool and a script that will do it.


Solution:4

On Netbeans there is a sort line tools plugin, which has the option to remove duplicates. Works perfectly if properties are one lined.

What is more, sorting the properties makes this file more readible.


Solution:5

It might be easiest just to write one: For each file, and for each property in that file, put the property key/value pair into a Map, but only after ensuring that the key is not already in the Map. If it is, print out the file name, the key, and the two values.


Solution:6

If you are using an IDE you might find a good tool among their plugins/features.

Eclipse has a ResourceBundle Editor plugin which manages properties files:

http://www.eclipseplugincentral.com/Web_Links-index-req-viewlink-cid-331.html

IntelliJ IDEA 8 and higher is also able to manage properties files and check for duplicate entries.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »