Hello Everyone. This is my First Blog and i would like to share my knowledge about the use of Configuration File in Java. Let's discuss about some basic ideas of Configuration File.
What is Configuration File in Java
The files are used to store data. Likewise the Configuration files are also plain text file in Java which contain key-value pairs that represent setting and configuration.
This files allow us to change the configuration without changing the source code.
These files are used to store information such as
External web Services
Database connection details
Application specific settings
Paths to external services
Why do we use Configuration File in Java
The main purpose of using a Configuration file is ,when we test a Java application, we separate the values from the code and keep it in a configuration file. According to the requirement we change the values of the variables without modifying anything in the source code.
It is easier to manage environment specific configuration by using different config files.
This files are also used for keeping sensitive data such as Username and Password, that developer won't expose in the source code.
Different ways to use Configuration File
There are several types of Configuration files such as
Properties Files (.properties)
XML Files (.xml)
JSON Files (.json)
YAML Files (.yaml)
How to store data in a config File
In a config file, we store the data as a key-value pairs and each key and value are represented as a String. In a Java project, under the resources folder the config files are stored.
How to create a config File in java
Before we create a configuration file, we have to decide which type of config file we need to create according to the requirement.
Steps:-
Under the resources folder create a new folder and open a new file in the folder and save with the extension .properties
Then add all the necessary key-value pairs and save it.
How to read a configuration file in Java
Steps:-
Create a package name as utils under src/test/java
Create a java class name as ConfigReader Under the package utils
This is the example of the program how to config file get loaded
package utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class ConfigReader {
Properties prop;
public Properties init_prop() {
prop= new Properties();
try {
FileInputStream inputProp = new FileInputStream("src/test/resources/config/config.properties");
prop.load(inputProp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
}
Explanation of above code :
init_prop() - It is a public method which returns properties object
prop - Initializing the value to prop by creating a new instance of properties and it holds the values loaded from properties file.
FileInputStream - To read the contents of the file, we use FileInputStream. While creating the new object of FileInputStream, passing the path of the file.
prop.load(inputProp)- This method is getting the values from inputProp and loading them into the prop object.
return prop - This return statement is returning the prop object after loading the properties from the File.
This is the another example of the program how to read the data from the config.properties file which is in the config folder
package utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ConfigReader {
private static Properties properties = new Properties();
static {
try (InputStream inputStream = ConfigReader.class.getResourceAsStream("/config/config.properties")) {
if (inputStream != null) {
properties.load(inputStream);
} else {
throw new RuntimeException("config.properties not found in the classpath");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String key) {
return properties.getProperty(key);
}
public static String getUserName() {
return properties.getProperty("username");
}
public static String getPassword() {
return properties.getProperty("password");
}
public static String getBaseUrl() {
return properties.getProperty("url");
}
public static String[] getAlphabetsList() {
return properties.getProperty("alphabetsList").split(",");
}
public static boolean isLoadDataRequired() {
return Boolean.parseBoolean(properties.getProperty("loadDataRequired"));
}
public static int getThreadPoolSize() {
return Integer.parseInt(properties.getProperty("threadPoolSize"));
}
public static int getTime() {
return Integer.parseInt(properties.getProperty("time"));
}
Explanation of above code :
static{} - In the above example , we are using static block to initialize the static fields and this static block is loaded once in the time of class load .
getResourceAsStream() - We are using this method which returns a InputStream that try to load the properties file which is present at the root of the class path.
if (inputStream != null) - According to the if condition , if the file was found, it will load the properties file or else throw the exception message.
getProperty(String key) - getProperty is a public static method with taking a parameter which is used to retrieve the property value and returning the value of the key.
getUserName() - This is a public static method which fetch the value from the properties object of key "username" and return it .
getPassword() - This is a public static method which fetch the value from the properties object of key "password" and return it .
getBaseUrl() - This is a public static method which fetch the value from the properties object of key "url" and return it .
isLoadDataRequired() - This is a public static method which fetch the value from the properties object of key "loadDataRequired" and return it .
getThreadPoolSize() - This is a public static method which fetch the value from the properties object of key "threadPoolSize" and return it .
getTime() - This is a public static method which fetch the value from the properties object of key "time" and return it .
Benefits of Using Java for Configuration File
Java config files are human-readable. It is easy to read and update and no require of extra libraries to open them.
These files are platform-independent.
These files are flexibility that allows us to modify application behavior.
These are useful for environments where code changes are not frequent.
Configuration Files provide single location to manage application settings.
These files separate the logic of the application from it's configuration.
we can use java.util.properties library of java to read these file.
We can use several ways to write the code of reading a config file. This is my understanding which i have shared my knowledge by writing this Blog. Hope you like it. Thank you for reading.