如何找到以确切单词开头的字符串

时间:2013-04-09 13:16:23

标签: java string apache-commons-config

我在gwt上有一个带GUI的应用程序。使用这个gui我想改变(这是子问题:如何更好地更改.properties文件(我知道如何使用org.apache.commons.configuration从它们读取属性,但不知道如何编辑(作为字符串或如何...))).properties文件。例如:我写了以下.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: 这是我使用config类,我想在其中更改配置文件:

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

3 个答案:

答案 0 :(得分:1)

尝试以下

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

希望有所帮助

答案 1 :(得分:1)

使用

加载道具
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();
            }
        }
    }

答案 2 :(得分:0)

回答原来的问题。

boolean String.startsWith(<prefix>)