如何找到以确切单词开头的字符串
问题描述:
我有一个应用程序GUI上gwt。使用这个GUI我想改变(这是一个子问题:如何更好地改变.properties文件(我知道如何使用org.apache.commons.configuration
读取它们的属性,但不知道如何编辑(如字符串或者如何...))).properties文件。例如:我写了下面的属性文件:如何找到以确切单词开头的字符串
################################################################################
# User permissions #
################################################################################
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 = *
,因此,如何添加keywords
的管理,例如?
UPD: 这里是我的配置类的工作中,我想更改配置文件:
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){
prop.setProperty("users." + customerId + ".keywords", prop.getProperty("users." + customerId + ".keywords") + ", " + keyword);
prop.
}
public void setRegexp(String customerId, String regexp)
{}
}
答
尝试以下
Properties prop = new Properties();
try {
//set the properties value
prop.setProperty("users.guest.keywords", "keyword");
prop.setProperty("users.guest.regexps", "regexps");
prop.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
希望它可以帮助
答
回答到你原来的问题。
boolean String.startsWith(<prefix>)
答
负载道具与
void getPropertiesFromFile(Properties props, String fileName)
{
try {
File file = new File(fileName) ;
if (!file.exists())
return ;
FileInputStream in = new FileInputStream(file) ;
try {
props.load(in) ;
} finally {
in.close() ;
}
} catch (Exception e) {
}
}
注:也许,你会想从一个文件但不ClassLoader.getResource方法加载它们();代码会稍微改变。
然后,遍历名称,找到你所需要的
for (String name : props.keys) {
if (name.startWith("users.admin.")) {
String value = props.get(name);
...
}
}
如果你要迭代过程中修改的道具,你应该换props.keys如下
for (String name : new HashSet(props.keys)) {
设置完毕后,存储它们
FileOutputStream fos = null;
try {
File file = new File("abc");
fos = new FileOutputStream(file);
props.store(fos, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
+0
关于您帖子中间的代码部分对值进行格式设置:据我所知,我找到了以users.admin开头的字符串,然后呢?至于我,我忘了说:我用户'属性道具',但'CompositeConfiguration道具' – 2013-04-09 14:02:46
这看起来像是最好的解决方案。以及如果我想要某些属性具有多个值,如:'users.guest.keywords = keyword1,keyword2,keyword3',该怎么办?是更好的'prop.setProperty(prop.getString(“users。”+ login +“.keywords”)+ new关键字)'? – 2013-04-09 13:38:10
您可以根据您的便利和应用程序要求 – Jabir 2013-04-09 13:57:24