What is a Properties File in Java?
In Java, a properties file is a simple text-based file used to store configuration or settings in key-value pairs. It provides a convenient way to externalize application-specific parameters that can be easily modified without modifying the source code.
Properties File is always storing information about the configuration parameters such as project configuration data such as URL, username, password, and project settings config like browser name, environment, and so on.
Properties files have a “.properties” extension.
Why do we need Properties File?
Properties files provide a convenient way to store configuration settings or parameters that can be easily modified without modifying the source code. It is not always a best practice to store the values as hard coded in the project.
With properties files, you can have different versions of the file tailored for specific environments (e.g., development, testing, production). Each environment can have its own set of configurations while sharing common structure and keys.
How to read data in a Properties File?
Steps to implement:
- Let us create a properties file (name it as config.properties)
- Add configuration data to the properties file.
- Create a java class to read the config file (name it as ConfigFileReader)
1.Create a Property File
Create a text file with the “.properties“ extension and define key-value pairs in it. For example, create a file named “config.properties”.

2. Add configuration data to the properties file.
We have added the configuration data to the property file in key-value pairs :
database.baseUrl = https://localhost
database.port = 8080
database.username = admin
database.password = Admin123
3. Create a java class to read the config file
You can load and read properties from this file using the Properties class. Below is an example of how to do this:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadPropertiesExample {
public static void main(String[] args) {
Properties properties = new Properties();
try {
InputStream inputStream = new FileInputStream("src/test/resources/config.properties");
// Load the properties file
properties.load(inputStream);
// Access properties
String dbUrl = properties.getProperty("database.baseUrl");
String dbPort = properties.getProperty("database.port");
String dbUser = properties.getProperty("database.username");
String dbPassword = properties.getProperty("database.password");
// Print properties to verify
System.out.println("Database URL: " + dbUrl);
System.out.println("Database Port: " + dbPort);
System.out.println("Database User: " + dbUser);
System.out.println("Database Password: " + dbPassword);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
FileInputStream: This class is used to read data from a file in the form of a sequence of bytes.load Method: The load method of the Properties class is used to read a properties file from an input stream.
The output of the above execution is

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!