Disclosure: This text could comprise affiliate hyperlinks. Whenever you buy, we could earn a small fee.
Two extra standard examples of properties file are jdbc.properties usually used to retailer database connection settings like URL, username, and password, and log4j.properties file which settings for java logging utilizing log4j.
There are additionally many frameworks that use Java properties information like Struts, Spring, Displaytag, and many others. One other benefit of utilizing properties information is you could characterize knowledge both in xml format or in properties format. xml format can be notably helpful in case you are sharing configuration or settings with some exterior software that understands solely XML.
On this article, we are going to see find out how to learn and write into Properties information in Java in each xml and textual content format.
And, In case you are new to Java Programming then I additionally suggest you undergo these Java on-line programs on Udemy to study Java in a greater and extra structured approach. This is likely one of the greatest and up-to-date programs to study Java on-line.
Studying values from Java Properties File
jdbc.username=take a look at
jdbc.password=unknown
jdbc.driver=com.mysql.jdbc.Driver
Code Examples of Studying Properties File in Java
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertyFileReader {
public static void predominant(String args[]) throws FileNotFoundException, IOException {
//Studying properties file in Java instance
Properties props = new Properties();
FileInputStream fis = new FileInputStream(“c:/jdbc.properties”);
//loading properties from a property file
props.load(fis);
//studying 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: take a look at
jdbc.driver: com.mysql.jdbc.Driver
In case you learn any property which isn’t specified within the properties file then you’ll props.getProperty() will return null.
Now let’s have a look at one other instance of studying property information from xml format. as you realize properties file in java could be represented in xml format and Java offers a handy methodology known as loadFromXML() to load properties from xml file. here’s a fast instance of parsing xml properties file and studying knowledge.
The way to Learn Property file in XML format – Java
On this part, we are going to see find out how to learn property file which is written in xml format. In case you see the code not many modifications as an alternative of Properties. load() we’re utilizing Properties.loadFromXML() after which the remainder of the stuff of getting property and printing its worth is similar as within the final instance.
By the way in which right here is our pattern java properties file in xml format, which outlined two entries with key jdbc.username and jdbc.password.
<?xml model=“1.0” encoding=“UTF-8” standalone=“no”?>
<!DOCTYPE properties SYSTEM “http://java.solar.com/dtd/properties.dtd”>
<properties> <entry key=“jdbc.username”>root</entry>
<entry key=“jdbc.password”>mysql</entry>
</properties>
And here’s a Java program that can learn XML properties file in Java:
//Studying properties file in Java instance
Properties props = new Properties();
FileInputStream fis = new FileInputStream(“c:/properties.xml”);
//loading properties from properties file
props.loadFromXML(fis);
//studying property
String username = props.getProperty(“jdbc.username”);
System.out.println(“jdbc.username: “ + username);
}
output:
jdbc.username: root
We’ve got seen find out how to learn properties information in java, In each textual content and xml format. Properties file are immensely useful for offering settings and configuration knowledge to any Java program. Textual content properties file can solely characterize linear values however xml properties file can even characterize hierarchical values which makes Properties file most popular alternative in logging frameworks.
Different Java tutorials you could like