2

I have properties file app.properties and it has 50 different properties. I am accessing those in my java class using

Properties prop = new Properties();
prop.load("app.properties");

System.out.prinltn(prop.getProperty("APPNAME"));

Actually, I want to get rid of accessing property like prop.getProperty("APPNAME"). Is there any best way in java to access properties.

I can declare all variables as static in java class.

static String appName = prop.getProperty("APPNAME");

Any other best way available?

rohitkadam19
  • 1,734
  • 5
  • 21
  • 39
  • If you store your variables in `Properties()` then not really. You could make the variables `static final String appName = ...;` which would give you code along the way of `String appName = Properties.appName;` – Emz Aug 17 '15 at 09:20
  • I would encapsulate your properties in a class with getters and setters. This way you can access the data as the right type and possibly change the name of the property and or getter without changing the other. – Peter Lawrey Aug 17 '15 at 09:20
  • Use a static initializer to load from the file – fge Aug 17 '15 at 09:21
  • No, I am not using spring. – rohitkadam19 Aug 17 '15 at 09:34

3 Answers3

3

I can suggest two approaches: 1. Define a utility method which will take String as parameter and return value from properties.

For Example:

public static String GetValue(String key) {
    return properties.getProperty(key);
}

And now you can use this function on callers

String value = GetValue("key"); // properties.getProperty("key");
  1. Define above method and in addition create one class Called Constants(or something suitable). Define all your Keys here as Static final variable.

    public class Constants { public static final String KEY = "key"; public static final String KEY2 = "key2"; }

and now make call for getting value using these variable instead of string:

String value = GetValue(KEY); //GetValue("key");

If you do only option 1, your code is becoming more readable. But I will recommend 2nd option, which is making your code readable as well as maintainable.

You can easily do following operation :

  1. Update property name
  2. No need to worry about mistyping key etc.
Kailas
  • 807
  • 1
  • 6
  • 20
0

You may use "resourceBundle" package as

First import the resourceBundle API:

import java.util.ResourceBundle;

Create an instance of your property file:

private static ResourceBundle resource = ResourceBundle.getBundle("app");

Now you can get the value of the property:

String appName = resource.getString("APPNAME");
Mahesh Kapoor
  • 244
  • 1
  • 4
-1

IMO, your approach of using static variables to hold the values is the best. The following structure was what I was using in a project for the same functionality.

package snippet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Constants {

    public static final String APPNAME;
    public static final String VERSION;
    public static final int DEFAULT_TIMEOUT;

    static {
        Properties p = new Properties();
        try {
            p.load(new FileInputStream("constants.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        APPNAME = p.getProperty("APPNAME");
        VERSION = p.getProperty("VERSION");
        DEFAULT_TIMEOUT = Integer.parseInt(p.getProperty("DEFAULT_TIMEOUT"));
    }
}

Of course, there were checks for NumberFormatException etc.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • Loading any resource from the static block is a bad idea, see [here](https://stackoverflow.com/questions/2070293/why-doesnt-java-allow-to-throw-a-checked-exception-from-static-initialization-b#comment54228450_15289277). – Conor Egan Apr 16 '23 at 19:43