0

I have an app with GUI on gwt. using this gui I'd like to change(and this is the subquestion: how this would be better to change the .properties file(I know how to read properties from them using org.apache.commons.configuration, but don't know how to edit(as string or how...))) the .properties file. for example: I've written the following .properties file:

################################################################################
# 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 = *

and so, how to add keywords for admin, for example?

UPD: here is my working with config class in which I want to change config file:

    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)
    {}
}
Nikitin Mikhail
  • 2,983
  • 9
  • 42
  • 67

3 Answers3

1

Try following

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

Hope it helps

Jabir
  • 2,776
  • 1
  • 22
  • 31
  • this looks like the best solution. and what if I want some properties to have multiple values, like: `users.guest.keywords = keyword1, keyword2, keyword3`? is it better `prop.setProperty(prop.getString("users." + login + ".keywords") + new keyword)`? – Nikitin Mikhail Apr 09 '13 at 13:38
  • You can format value as per your convenience and application requirements – Jabir Apr 09 '13 at 13:57
1

Load props with

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

NOTE: Perhaps, you would want to load them not from a file but with classLoader.getResource(); The code will change slightly.

Then, iterate over names and find what you need

for (String name : props.keys) {
   if (name.startWith("users.admin.")) {
       String value = props.get(name);
       ...
   }
}

If you are going to modify props during the iteration you should wrap props.keys as follows

for (String name : new HashSet(props.keys)) {

After you are done, store them

    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();
            }
        }
    }
Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • about the part of code which is in the middle of your post: as I understand I found the string which begins with users.admin and what then? As for me, i've forgot to say: I user `properties props`, but `CompositeConfiguration prop` – Nikitin Mikhail Apr 09 '13 at 14:02
0

Answer to your original question.

boolean String.startsWith(<prefix>) 
IndoKnight
  • 1,846
  • 1
  • 21
  • 29