Load values from properties file in Java
Well, there are couple of ways to load values from properties file in Java. To start with, we will discuss the simplest and most common way of doing it.
Create an instance of File class.
Create an instance of File class.
File file = new File("<filelocation>/<filename>");
Create an instance of FileInputObject.
FileInputObject input = new FileInputObject(file);
Create an instance of Properties class from java.util package, which does the real work.
Properties prop = new Properties();
Load the properties to the Properties instance
prop.load(input);
Now, you are good to go.
System.out.println(prop.getProperty("<propname-from-file>));
Let's put it together and into it's shorter form:
Properties prop = new Properties();
prop.load(new FileInputObject("<filelocation>/<filename>"));
System.out.println(prop.getProperty("<propname-from-file>));
Just 3 lines of code and you are done.
One of the limitation of this approach, is that the values are loaded at startup. If you need to update the value, server need to be started.
In the upcoming blogs, we will be discusssing the other approaches.















