Warning Newbie here.

I am trying to read a properties file and set the values to be used throughout the code.

I can open the file and get the values. I am struggling on making the accessible for other classes/methods.

Here is what I have so far:

Properties configProp = new Properties();
  InputStream cfgFile = new FileInputStream(appconfig);
  configProp.load(cfgFile);
  cfgFile.close();
  String URL = configProp.getProperty("runtime.url");
  String USER = configProp.getProperty("runtime.userid");
  String PASS = configProp.getProperty("runtime.passwd");

I want to use these values in other methods and classes.

Thank you for any help!

depends if is

appconfig

correct path, if wouldn't be this properties file amnendable, put that to package and compile to the jar

Appconfig is already configured

private static final String appconfig = "/Users/data/info.properties";

I can get the information in the current method but I cannot use it in other methods.

public class Processor {

        private static String URL;
        private static String USER;
        private static String PASS;
        private static String TID;
        private static String S_INDX_s;
        private static String M_INDX_s;
        private static int S_INDX;
        private static int M_INDX;
        private static String DAYS_s;
        private static int DAYS;
        private static String duser;
        private static String dwhost;
        private static String dpassword;
        private static String DIR;
        private static Statement statement;
        private static final Logger log  = Logger.getLogger(Processor.class);
        private static final String DATE_FORMAT = "yyyy-MM-dd";
        private static final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        private static String subject;
        private static String body;
        private static final String appconfig = "/Usersdata/infol.properties";

private static void configFile() throws FileNotFoundException, IOException  {

  Properties configProp = new Properties();
  InputStream cfgFile = null;
        try {
            cfgFile = new FileInputStream(appconfig);
        } catch (FileNotFoundException ex) {
             log.error(ex);
        }
        try {
            configProp.load(cfgFile);
        } catch (IOException ex) {
            log.error(ex);
        }
        try {
            cfgFile.close();
        } catch (IOException ex) {
             log.error(ex);
        }

  URL = configProp.getProperty("runtime.ga.url");
  USER = configProp.getProperty("runtime.ga.userid");
  PASS = configProp.getProperty("runtime.ga.passwd");
  TID = configProp.getProperty("runtime.ga.id");
  S_INDX_s = configProp.getProperty("runtime.ga.sindex");
  M_INDX_s= configProp.getProperty("runtime.ga.mindex");
  DAYS_s = configProp.getProperty("runtime.ga.days");
  duser = configProp.getProperty("runtime.dwuser");
  dpassword = configProp.getProperty("runtime.dwpasswd");
  dwhost = configProp.getProperty("runtime.dwhost");
  DIR = configProp.getProperty("runtime.dir");
  DAYS = new Integer(DAYS_s);
  S_INDX = new Integer(S_INDX_s);
  M_INDX = new Integer(M_INDX_s);
};
public static void main(String[] args) throws Exception {

    configFile();
   
blahblahblah more code
    };
};

If I call it in the main method, it seems to do what I need. Logically, I would say - hey, does what I want... But, is there a better way of doing this? Am I doing it correctly?

A couple of observations:
1. You have 3 consecutive try/catch structures. If the first one fails you log the error - good - but then go on to try to access the file anyway.
It would be better to put all that IO code in a single try/catch so that if any one step fails it doesn't attempt the rest. Alternatively, return; after logging an error, since there's no point continuing with the method.
2. Now how to access these values from other classes?
2a. Write public getXXX methods for each of them
2b. Make the variables public final so anyone can use them, but not change them. This requires that they all get set in the class's constructor(s).
2c. Make the Properties object available to other classes, since the syntax to retrieve a property from it is so easy. Then you don't need to publish a dozen values separately, and if/when new properties are added the code doesn't change.
2c(1) ... by writing a public getProperties method
2c(2) ... by making configProp public final (although this will allow users to change properties directly.
2d. Write a single public getProperty(String key) method that just delegates to the Properies object, and get rid of the dozen separate variables. This has all the advantages of 2c, but proptects the values from being changed.

I personally would probably go for 2d..

Regardless of which of those routes you take you also have to chose between making all that stuff static or instance based. Most experts will have a preference for instance based, but then you have the problem of how other classes will get access to the instance. However, if your app can have multiple users with separate preferences then this is the only way to go.
If there is only one set of preferences then I personally would say "stuff the purists" and make it all static.

Edited 5 Years Ago by JamesCherrill: n/a

Thank you for the info! I hate being such a newbie and asking but do you have any examples I can check out? If not, I will continue to google :D

Adding one more option, you can write a "PropsLoader" class and give classes access to that. This allows you to do things like load the properties on startup and keep them in memory in a centralized location, and to write back to them in an organized fashion (ie, set the property during the run, and only write it out on exit or on demand). You might also be clever and do a "load-once" policy: the first time you need a property, you load it, and then you store it (hello, HashMap) for future requests.

This article has been dead for over six months. Start a new discussion instead.