Throw exception when a property is duplicated in a properties file

1.3k views Asked by At

How can I throw an Exception when a properties file contains a duplicate property? Here is an example demonstrating this situation:

# Properties-file

directory=D:\\media\\D-Downloads\\Errorfile\\TEST_A
directory=D:\\media\\D-Downloads\\Errorfile\\TEST_B
#directory=D:\\media\\D-Downloads\\Errorfile\\TEST_C
4

There are 4 answers

3
Ralf Kleberhoff On BEST ANSWER

I suppose you are reading the file with something like Properties.load(). It sets the parameter internally using put(key, value). You can override that method to get the desired behaviour like e.g.

new Properties() {
    @Override
    public synchronized Object put(Object key, Object value) {
        if (get(key) != null) {
            throw new IllegalArgumentException(key + " already present.");
        }
        return super.put(key, value);
    }
}.load(...);

EDIT:

Integrating this into the OP's code:

File propertiesFile = new File("D:/media/myProperties.properties");
Properties properties = new Properties() {
    @Override
    public synchronized Object put(Object key, Object value) {
        if (get(key) != null) {
            // or some other RuntimeException you like better...
            throw new IllegalArgumentException(key + " already present.");
        }
        return super.put(key, value);
    }
}
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(propertiesFile))) {
  properties.load(bis);

} catch (IllegalArgumentException ex) {
  //
}

By the way, why would you want to catch the exception? I'd not continue a program if its configuration is corrupt (maybe catching at top-level to log the event). But exception-handling is a different topic...

(EDIT: my original code samles didn't compile, I corrected them)

0
user14448172 On

As mentioned here Tool to find duplicate keys and value in properties file

" There are two nice tools that I use

unival npm package: this is a command line tool to detect duplicate keys, values or lines.

npm command to install the package: npm i unival

Link: https://www.npmjs.com/package/unival

unival extension: if you use vscode, this is a extremely helpful extension to detect duplicates on the fly. "

The best way is to have a test to run unival command, this will prevent duplicate values going to properties file

0
DwB On

The Ralf Kleberhoff answer is correct; however, I would not use an anonymous class. It seems likely that you want to use this functionality more than once, so I would create a class that extends Properties and override the put as did Ralf

Note that the put method is from the Hashtable class which Properties extends.

Here is an example (I didn't try to compile it):

public class UniqueProperties extends Properties
{
    @Override
    public synchronized String put(Object key, Object value)
    {
        if (get(key) != null)
        {
            throw new IllegalArgumentException(key + " already present.");
        }

        super.put(key, value);
    }
}
0
Toti On

Here is how I am loading the properties:

        File propertiesFile = new File("D:/media/myProperties.properties");
    Properties properties = new Properties();
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(propertiesFile))) {
      properties.load(bis);

    } catch (Exception ex) {
      //
    }

@Ralf How can I adapt your code?