【问题标题】:How to find the string which begins with the exact word如何找到以确切单词开头的字符串
【发布时间】:2013-04-09 13:16:22
【问题描述】:

我在 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

更新: 这是我要更改配置文件的配置类:

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

【问题讨论】:

    标签: java string apache-commons-config


    【解决方案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();
            }
    

    希望对你有帮助

    【讨论】:

    • 这看起来是最好的解决方案。如果我希望某些属性具有多个值,例如:users.guest.keywords = keyword1, keyword2, keyword3,该怎么办? prop.setProperty(prop.getString("users." + login + ".keywords") + new keyword) 更好吗?
    • 您可以根据您的便利性和应用要求格式化值
    【解决方案2】:

    加载道具
    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,你应该如下包装 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();
                }
            }
        }
    

    【讨论】:

    • 关于您帖子中间的代码部分:据我所知,我找到了以 users.admin 开头的字符串,然后呢?至于我,我忘了说:我用户properties props,但是CompositeConfiguration prop
    【解决方案3】:

    回答你原来的问题。

    boolean String.startsWith(<prefix>) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多