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
}
}
}
}
3 comments:
This is easier methinks
/**
* 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().
//
key = null;
}
return super.put(key, value);
}
}
Can I simply just say what a relief to discover somebody that truly understands what they're talking about on the web. You definitely know how to bring an issue to light and make it important. A lot more people ought to check this out and understand this side of the story. I was surprised you're not more popular because you most certainly have the gift. facebook.com login
As long because the loan is backed and it meets your terms, you should feel confident in signing it. mortgage calculator canada At the identical time, she wasn't pushy and allowed us time to produce decisions in what we wished to do. mortgage calculator
Post a Comment