Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read properties file in java having particular string

I am using one .properties file. In that I have following config parameters :

Appnameweb = app1
Appnamemobile = app2
Appnameweb1 = app3

There can be many config param starting with Appname provided by user. How to read all properties file parameters in which key will contain particular String like in this case Appname?

like image 390
iRunner Avatar asked Dec 06 '22 02:12

iRunner


2 Answers

Generally take a look on javadoc of java.util.Properties. To make you life easier I will give you this code snippet that can help you to start:

Properties props = new Properties();
props.load(new FileInputStream("myfile.properties"));
for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
    String name = (String)e.nextElement();
    String value = props.getProperty(name);
    // now you have name and value
    if (name.startsWith("Appname")) {
        // this is the app name. Write yor code here
    }
}
like image 174
AlexR Avatar answered Dec 28 '22 06:12

AlexR


Properties props = new Properties();
props.load(new FileInputStream("file.properties"));
Enumeration<String> e = props.getNames();
List<String> values = new ArrayList<String>();
while(e.hasMoreElements()) {
    String param = (String) e.nextElement();
    if(param != null && param.contains("Appname")) { 
         values.add(props.getProperty(param));
    }   
}
like image 40
aviad Avatar answered Dec 28 '22 07:12

aviad