简体   繁体   English

当属性在属性文件中重复时抛出异常

[英]Throw exception when a property is duplicated in a properties file

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

I suppose you are reading the file with something like Properties.load() .我想你正在阅读类似Properties.load()的文件。 It sets the parameter internally using put(key, value) .它使用put(key, value)在内部设置参数。 You can override that method to get the desired behaviour like eg您可以覆盖该方法以获得所需的行为,例如

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:将其集成到 OP 的代码中:

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) (编辑:我的原始代码示例没有编译,我更正了它们)

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. unival npm 包:这是一个命令行工具,用于检测重复的键、值或行。

npm command to install the package: npm i unival安装包的 npm 命令:npm i unival

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

unival extension: if you use vscode, this is a extremely helpful extension to detect duplicates on the fly.统一扩展:如果您使用 vscode,这是一个非常有用的扩展,可以即时检测重复项。 "

The best way is to have a test to run unival command, this will prevent duplicate values going to properties file最好的方法是进行测试以运行 unival 命令,这将防止重复值进入属性文件

The Ralf Kleberhoff answer is correct; Ralf Kleberhoff 的答案是正确的; 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似乎您想多次使用此功能,因此我将创建一个类来扩展Properties并像 Ralf 一样覆盖put

Note that the put method is from the Hashtable class which Properties extends.请注意, put 方法来自Properties扩展的Hashtable类。

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);
    }
}

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? @Ralf 我该如何调整您的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM