Read properties file in java having particular string

iRunner picture iRunner · May 7, 2014 · Viewed 21.7k times · Source

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?

Answer

AlexR picture AlexR · May 7, 2014

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