.properties
is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.
Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key/map
), and the other storing the value.
Below is a sample Java program which demonstrate you how to retrieve/read
config.properties values in Java. For update
follow this tutorial.
Another must read
: Read config.properties value using Spring “singleton” Scope in your Java Enterprise Application
We will create 3 files:
- CrunchifyReadConfigMain.java
- CrunchifyGetPropertyValues.java
config.properties
file
Main Class (CrunchifyReadConfigMain.java) which will call getPropValues()
method from class CrunchifyGetPropertyValues.java
.
Let’s get started:
Step-1: Create config.properties
file.
- Create Folder “
resources
” underJava Resources
folder if your project doesn’t have it. - create
config.properties
file with below value.
/Java Resources/config.properties file content:
#Crunchify Properties user=Crunchify company1=Google company2=eBay company3=Yahoo
Step-2
Create file CrunchifyReadConfigMain.java
package crunchify.com.tutorial; import java.io.IOException; /** * @author crunchify.com * */ public class CrunchifyReadConfigMain { public static void main(String[] args) throws IOException { CrunchifyGetPropertyValues properties = new CrunchifyGetPropertyValues(); properties.getPropValues(); } }
Step-3
Create file CrunchifyGetPropertyValues.java
package crunchify.com.tutorial; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.Properties; /** * @author crunchify.com * */ public class CrunchifyGetPropertyValues { String result = ""; InputStream inputStream; public String getPropValues() throws IOException { try { Properties prop = new Properties(); String propFileName = "config.properties"; inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); if (inputStream != null) { prop.load(inputStream); } else { throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); } Date time = new Date(System.currentTimeMillis()); // get the property value and print it out String user = prop.getProperty("user"); String company1 = prop.getProperty("company1"); String company2 = prop.getProperty("company2"); String company3 = prop.getProperty("company3"); result = "Company List = " + company1 + ", " + company2 + ", " + company3; System.out.println(result + "\nProgram Ran on " + time + " by user=" + user); } catch (Exception e) { System.out.println("Exception: " + e); } finally { inputStream.close(); } return result; } }
Step-4
Run CrunchifyReadConfigMain
and checkout result.
Company List = Google, eBay, Yahoo Program Ran on Mon May 13 21:54:55 PDT 2013 by user=Crunchify
As usually happy coding and enjoy..!! Do let me know if you see any exception. List of all Java Tutorials.
Are you running above program in IntelliJ IDE and getting NullPointerException?
Please follow below tutorial for fix.
Hey Dude,
how can i use the strings which was created in CrunchifyGetPropertyValues also in CrunchifyReadConfigMain.java? I have no clue, how i can have access to it.
Thx
Toni
On load I am getting compile time error “The method is undefined for the type properties” and telling to cast it but after casting ti I am still getting compile time error
Hi Harsh. Could you please share detailed exception or screenshot?
Working now, I was importing the wrong package……Thanks for the code
Good to hear that Harsh. Happy coding.
Getting file not found Exception
NICE CODE
Nice post.. it helped me alot but I don’t want to use new CrunchifyGetPropertyValues(); every time. I want to load the property file only once and want to use it. and
getPropValues() will be static . right now I am not able to do so can anyone please help me
Very valid point Rajesh. Usually I do use Singleton object for Config property. Just initialize once and use it everywhere in your project at runtime with getInstance().
Sample singleton example: https://crunchify.com/thread-safe-and-a-fast-singleton-implementation-in-java/
I hope this helps 🙂
Thanks App Shah for reply. but getClass() is not static member I can not call in static getInstanceMethod
Hi Rajesh – agree. what is mean is – Above tutorial will not work as it for your need. Try writing program in different singleton way.
hii, how can i write database connection parameter like(driver class,username,password) from multiple database like oracle and mysql in a same properties file
May be using different properties name? 🙂
You should close the inputStream. Thanks!
Thanks Ambarish for tips. Updated above tutorial.
Isn’t it the same than using ResourceBundle.getBundle storing the properties in a bundle.properties?
Hi there – both has similar functionality. I’ll create new tutorial on ResourceBundle so user will have more options to choose between 🙂
Hello, This work perfect. but I have a problem trying to do an “while” I had my “prop.load(String)” inside in my “while” and only print the last item in the query. Any help to modify or print the loop?
“prop.load(inputStream);”
dis statement must come after the null check for inputstream…i guess
Good point. Fixed NullPointerException bug and updated above tutorial.
This was really helpful for me. Thank you so much!
I’m glad it worked for you 🙂
What if you wanted a different property file for each server that it’s deployed on. For example I have a dev/qa folder with the properties file in it. How do I tell it “hey im on this server” by using this load properties method?
Good point. I do have that program already.. Will publish tutorial for the same may be in a week.
Which font do you use as your eclipse default editor font?Tell me please, thks.
Hi there, I use just default eclipse font in Editor. No modification at all 🙂
Hi,
I tried the code, I am getting the below exception…can you pls guide?
Make sure to have config file in proper folder. Also, could you please share complete log? Or Screenshot?
The resource folder needs to be created as a new “source folder” rather than just a new folder, otherwise you will get the above errors.
Thanks John.
Thanks for the info.
how to do that?
Keep the property file inside bin folder which worked for me
Hi Saurabh – ideally we should put all properties file under
/resources
folder. IdeallyJava Resources
folder is the one which by default be included into classpath.