How to Create a Config.Properties file in Eclipse and Load it
In here we are going to see how to create a config.properties file in Eclipse and how to load it and bring it in use. You see it’s a good idea to place all your properties in a different file. So that you retrieve your data using your code, so that nothing is hard coded.
It is a good coding practice to use a config.properties file where you can store all your fixed client requirements and use your code to call each values using the getProperty() method.
In here, we are going to see exactly that – how to create a config.properties file in Eclipse, load it and then use it effectively.
How to Create a config.properties file in Eclipse
Step 1: Right click on the folder you wish to place your config file in and then click on New> File
Step 2: Then in the New File dialog box, in the text box File Name, type the name as “config.properties”:
Step 3: Click on Finish
It will create a blank properties file for you where you can now store all your properties to be extracted whenever you need an input somewhere in your code.
I will just go ahead and add some properties that I may need for my code in the long run.
Step 4: Provide properties in your config.properties file:
Now you can load these properties to be used in your code. Loading properties is important or your Eclipse would treat it as just an unclaimed file.
How to Load Properties in Eclipse
This is the next important step while working with config.properties file. You need to load it as well. Here are the steps:
First of all, we need to initialize properties.
Step 1: Type the following in your class:
Properties config;
If you notice an error popping up at Properties, just import the following:
import java.util.Properties;
or put your mouse cursor on Properties and it will flare options to consider. Click on the option that says java.util.
Step 2: Now instantiate it by calling the constructor of Properties.
config = new Properties();
There! It has been instantiated as well.
Step 3: Now you need to make Eclipse take the inputs from it using an Input Stream or really you can use any method described in how to read a file tutorial.
For that we will initialize FileInputStream first and then instantiate it in a similar manner:
First we will initialize it.
FileInputStream fis;
Step 4: Time to instantiate it:
fis = new FileInputStream(FILEPATH);
Replace the FILEPATH with the actual path of your config file.
Make sure you import java.io for it to work:
You might also have to use try catch to handle exceptions:
If you aren’t sure how to handle exceptions, feel free to check out that tutorial as well.
Step 5: Now is the time to actually load the config.properties file.
You can do so by simply adding a piece of code which takes InputStream as a parameter.
config.load(fis);
The above line will load the file input stream
So your code might look like this:
How to Use the Properties in Your Code
Now let’s see how we are going to use it in our code.
In order to call the properties all you have to do is use the Properties instance to call and use the method getProperty() to obtain the said property.
Here’s how:
Append the following in your code:
As you can see we are simply using the getProperty() method of Properties class to retrieve information from the config.properties file.
Isn’t that simple?
Run the program and you will get the following result:
Noiiice! Hope this page was useful.
I’m having problem calling properties in another class. I want to use .properties as repository which I will use in different classes. Could you help me out please?
What problem are you facing? Please elaborate.