2013-04-09 53 views
0

我有一個應用程序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) 
    {} 
} 

回答

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

希望它可以幫助

+0

這看起來像是最好的解決方案。以及如果我想要某些屬性具有多個值,如:'users.guest.keywords = keyword1,keyword2,keyword3',該怎麼辦?是更好的'prop.setProperty(prop.getString(「users。」+ login +「.keywords」)+ new關鍵字)'? – 2013-04-09 13:38:10

+0

您可以根據您的便利和應用程序要求 – Jabir 2013-04-09 13:57:24

0

回答到你原來的問題。

boolean String.startsWith(<prefix>) 
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(); 
      } 
     } 
    } 
+0

關於您帖子中間的代碼部分對值進行格式設置:據我所知,我找到了以users.admin開頭的字符串,然後呢?至於我,我忘了說:我用戶'屬性道具',但'CompositeConfiguration道具' – 2013-04-09 14:02:46

相關問題