Reading and writing properties file in Java is a little different than reading or writing text file in Java or reading xml files in Java using xml parsers like DOM because Java provides the special provision to the properties file. For those who are not familiar with Properties files in java, It is used to represent configuration values like JDBC connectivity parameter or user preference settings and has been a primary source of injecting configuration on Java applications. Properties file in Java is a text file that stores data in the form of key and value, key being known as property. Java allows you to read the value of the property by providing its name which makes it easy to access configuration settings. Many application like Spring and Spring Boot also uses property file for configurations for example application.properties is commonly used to setup Spring boot.
Two more popular examples of properties file are jdbc.properties often used to store database connection settings like URL, username, and password, and log4j.properties file which settings for java logging using log4j.
There are also many frameworks that use Java properties files like Struts, Spring, Displaytag, etc. Another advantage of using properties files is that you can represent data either in xml format or in properties format. xml format will be particularly useful if you are sharing configuration or settings with some external tool that understands only XML.
In this article, we will see how to read and write into Properties files in Java in both xml and text format.
And, If you are new to Java Programming then I also recommend you go through these Java online courses on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Two more popular examples of properties file are jdbc.properties often used to store database connection settings like URL, username, and password, and log4j.properties file which settings for java logging using log4j.
There are also many frameworks that use Java properties files like Struts, Spring, Displaytag, etc. Another advantage of using properties files is that you can represent data either in xml format or in properties format. xml format will be particularly useful if you are sharing configuration or settings with some external tool that understands only XML.
In this article, we will see how to read and write into Properties files in Java in both xml and text format.
And, If you are new to Java Programming then I also recommend you go through these Java online courses on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Reading values from Java Properties File
Here is a sample code example of reading a properties file in Java. In this example, we will read a property from jdbc.properties file which contains database connection settings, you might have already used that.
Anyway, it contains username, password, and jdbc driver class name as a key-value format. We will read these values from the Java program using java.util.Properties class which represents properties file in Java program.
jdbc.username=test
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver
Code Examples of Reading Properties File in Java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertyFileReader {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties file in Java example
Properties props = new Properties();
FileInputStream fis = new FileInputStream("c:/jdbc.properties");
//loading properties from a property file
props.load(fis);
//reading property
String username = props.getProperty("jdbc.username");
String driver = props.getProperty("jdbc.driver");
System.out.println("jdbc.username: " + username);
System.out.println("jdbc.driver: " + driver);
}
}
Output:
jdbc.username: test
jdbc.driver: com.mysql.jdbc.Driver
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertyFileReader {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties file in Java example
Properties props = new Properties();
FileInputStream fis = new FileInputStream("c:/jdbc.properties");
//loading properties from a property file
props.load(fis);
//reading property
String username = props.getProperty("jdbc.username");
String driver = props.getProperty("jdbc.driver");
System.out.println("jdbc.username: " + username);
System.out.println("jdbc.driver: " + driver);
}
}
Output:
jdbc.username: test
jdbc.driver: com.mysql.jdbc.Driver
If you read any property which is not specified in the properties file then you will props.getProperty() will return null.
Now let's see another example of reading property files from xml format. as you know properties file in java can be represented in xml format and Java provides a convenient method called loadFromXML() to load properties from xml file. here is a quick example of parsing xml properties file and reading data.
How to Read Property file in XML format – Java
In the earlier section, we have seen a sample working code example of reading properties file (.properties) but as I said earlier you can also define property in xml format this method is also very popular among various Java logging frameworks e.g. log4j, and also among others.
In this section, we will see how to read property file which is written in xml format. If you see the code not many changes instead of Properties. load() we are using Properties.loadFromXML() and then the rest of the stuff of getting property and printing its value is the same as in the last example.
By the way here is our sample java properties file in xml format, which defined two entries with key jdbc.username and jdbc.password.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties> <entry key="jdbc.username">root</entry>
<entry key="jdbc.password">mysql</entry>
</properties>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties> <entry key="jdbc.username">root</entry>
<entry key="jdbc.password">mysql</entry>
</properties>
And here is a Java program that will read XML properties file in Java:
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties file in Java example
Properties props = new Properties();
FileInputStream fis = new FileInputStream("c:/properties.xml");
//loading properties from properties file
props.loadFromXML(fis);
//reading property
String username = props.getProperty("jdbc.username");
System.out.println("jdbc.username: " + username);
}
output:
jdbc.username: root
//Reading properties file in Java example
Properties props = new Properties();
FileInputStream fis = new FileInputStream("c:/properties.xml");
//loading properties from properties file
props.loadFromXML(fis);
//reading property
String username = props.getProperty("jdbc.username");
System.out.println("jdbc.username: " + username);
}
output:
jdbc.username: root
We have seen how to read properties files in java, In both text and xml format. Properties file are immensely helpful for providing settings and configuration data to any Java program. Text properties file can only represent linear values but xml properties file can also represent hierarchical values which makes Properties file preferred choice in logging frameworks.
Other Java tutorials you may like
8 comments :
Shouldn't your example include the use of the class loader? That seems to be the way many/most applications find and load the properties files.
Hi Patrick, Sorry I couldn't get your point. Can you elaborate more, are you saying loading properties file in static initializer block ?
In order to load or read properties file i Java , properties file must be in classpath otherwise you will not be able to read properties.
Hopefully you will find the good tutorial for How to read Properties File in Java which will help you to get database connection parameters from the property file.
How to read Properties File in Java
Thanks Java Srilankan support for liking this Java tutorial on properties file and sharing your How to read properties file in Java tutorial.If you Java tutorial here please consider sharing in your blog and group. keep in touch
Javin
how do i load a property file from relative path.here you have user static path, but what if it is a relative path like different folder within the project folder?
If you need a tool to translate .properties or XML files, check out the localization management platform POEditor.
Using it is free up to 1000 strings, which should be enough for localizing a small app into a few languages.
Good tuto, it saves me
Post a Comment