Friday, March 1, 2013

How to detect the duplicate keys in .properties file

How to detect the duplicate keys in .properties file.


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class PropertyDuplicateCheck {

 /**
  * @param args
  */
 public static void main(String[] args) {
  FileInputStream fis = null;
  String EQUALs = "=";
  try {
   fis = new FileInputStream("Z:\\dsp-jarpaths.properties");
   Set<String> set = new TreeSet<String>();
   Map<String, Integer> map = new TreeMap<String, Integer>();

   BufferedReader br = new BufferedReader(new InputStreamReader(fis));
   String strLine;
   String keyValuePair[] = null;
   System.out.println("Unparsed or commented Lines:");
   while ((strLine = br.readLine()) != null) {
    if (strLine.contains(EQUALs)) {
     keyValuePair = strLine.split(EQUALs);
     String key = keyValuePair[0];

     if (!set.add(key)) {
      if (map.containsKey(key)) {
       map.put(key, map.get(key).intValue() + 1);
      } else {
       map.put(key, 2);
      }
     }

    } else {
     System.out.println(strLine);
    }
   }

   System.out.println("Print duplicate entry with duplication count:"
     + map);

  } catch (Exception e) {
   System.err.println("Error: " + e.getMessage());
  } finally {
   try {
    fis.close();
   } catch (Exception e) {
    // do nothing
   }
  }

 }

}


Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com