我有一个应用程序,我根据一些规则(现有一些关键字或正则表达式)过滤邮件。这些规则将存储在.properties文件中(因为它们必须是持久的)。我已经想出如何从这个文件中读取数据。这是代码的一部分:
public class Config {
private static final Config ourInstance = new Config();
private static final CompositeConfiguration prop = new CompositeConfiguration();
public static Config getInstance() {
return ourInstance;
}
public Config(){
}
public synchronized void load() {
try {
prop.addConfiguration(new SystemConfiguration());
System.out.println("Loading /rules.properties");
final PropertiesConfiguration p = new PropertiesConfiguration();
p.setPath("/home/mikhail/bzrrep/DLP/DLPServer/src/main/resources/rules.properties");
p.load();
prop.addConfiguration(p);
} catch (ConfigurationException e) {
e.printStackTrace();
}
final int processors = prop.getInt("server.processors", 1);
// If you don't see this line - likely config name is wrong
System.out.println("Using processors:" + processors);
}
public void setKeyword(String customerId, String keyword){
}
public void setRegexp(String customerId, String regexp)
{}
}
如您所见,我将为某些属性添加值。这是.properties文件本身:
users = admin, root, guest
users.admin.keywords = admin
users.admin.regexps = test-5, test-7
users.root.keywords = root
users.root.regexps = *
users.guest.keywords = guest
users.guest.regexps =
我有一个GUI,供用户在此配置中添加关键字和正则表达式。那么,如何实现方法setKeyword和setRegexp?
答案 0 :(得分:1)
我发现最简单的方法是将属性的当前值读取到String [],添加一个新值并设置属性。
props.setProperty(fieldName, values);