Reading Data from Properties Files in Java
A properties file in Java is a simple text file used to store configuration data in the form of key–value pairs. These files typically end with the extension .properties and are widely used for:
Application configuration
Internationalization (i18n)
Database connection settings
Environment-based values
1. What Does a Properties File Look Like?
Example: config.properties
app.name=MyApplication
app.version=1.0
db.url=jdbc:mysql://localhost:3306/test
db.user=root
db.password=secret
2. Using Properties Class to Read Properties
Java provides the Properties class (in java.util) for loading and reading property files.
Basic Steps
Create a Properties object
Load the .properties file
Retrieve values using getProperty()
3. Reading from Properties – File System Approach
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertiesExample {
public static void main(String[] args) {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream("config.properties")) {
props.load(fis);
String appName = props.getProperty("app.name");
String version = props.getProperty("app.version");
System.out.println("Application: " + appName);
System.out.println("Version: " + version);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Reading from Classpath (Recommended)
If your .properties file is inside src/main/resources, you can load it using ClassLoader.
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ClasspathPropertiesExample {
public static void main(String[] args) {
Properties props = new Properties();
try (InputStream input = ClasspathPropertiesExample.class
.getClassLoader()
.getResourceAsStream("config.properties")) {
if (input == null) {
System.out.println("Sorry, unable to find config.properties");
return;
}
props.load(input);
System.out.println(props.getProperty("app.name"));
System.out.println(props.getProperty("db.url"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
This approach works well in Maven or Gradle projects.
5. Default Values with getProperty()
You can provide a default value in case the key doesn't exist.
String mode = props.getProperty("app.mode", "development");
6. Loading XML-Based Properties Files
Java also supports XML format:
Example: config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>XML Properties Example</comment>
<entry key="app.name">MyApp</entry>
<entry key="app.version">2.0</entry>
</properties>
Read XML properties:
props.loadFromXML(new FileInputStream("config.xml"));
7. Writing Data to Properties Files
try (FileOutputStream output = new FileOutputStream("config.properties")) {
props.setProperty("new.key", "newValue");
props.store(output, "Updated properties");
}
8. Using Properties in a Real Project (Example: Database)
db.properties
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=pass123
Java code:
String url = props.getProperty("db.url");
String user = props.getProperty("db.username");
String pass = props.getProperty("db.password");
Connection conn = DriverManager.getConnection(url, user, pass);
Summary
Topic Summary
Properties file Key–value configuration storage
Java API Use java.util.Properties
Loading methods FileInputStream, ClassLoader, XML
Access values getProperty(key) or with default
Common uses Config settings, environment values, localization
Learn Selenium with JAVA Training in Hyderabad
Read More
Creating Utility Classes for Common Selenium Functions
Java OOP Concepts for Selenium Testers
Working with Collections in Java for Selenium Testing
Handling Exceptions in Selenium Test Scripts
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments